Android Browser.BOOKMARKS_URI does not work on all devices. How to find out the correct uri for a given device?

后端 未结 2 787
南笙
南笙 2020-12-10 05:53

I am trying to use Android Browser.BOOKMARKS_URI to CRUD device bookmarks from within my app ( https://play.google.com/store/apps/details?id=com.elementique.web )

It

相关标签:
2条回答
  • 2020-12-10 06:37

    Is there a way to 'list' all available content URI (content://...) on a devices?

    There's an opensource APP called "Content Provider Helper" in Play Store that can list all available content:// URI on the device. It uses PackageManager.GET_PROVIDER

    Here's the respective class that searches all available content provider:

    SearchProviderTask.java

    package com.jensdriller.contentproviderhelper.task;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Locale;
    import android.content.Context;
    import android.content.pm.PackageInfo;
    import android.content.pm.PackageManager;
    import android.content.pm.ProviderInfo;
    import android.net.Uri;
    
    public class SearchProvidersTask extends DialogAsyncTask<Uri, Void, List<String>> {
        public SearchProvidersTask(Context context) {
            super(context);
        }
    
        @Override
        protected List<String> doInBackground(Uri... params) {
            List<String> contentProviders = new ArrayList<String>();
    
            try {
                PackageManager pm = mContext.getPackageManager();
                for (PackageInfo pack : pm.getInstalledPackages(PackageManager.GET_PROVIDERS)) {
                    ProviderInfo[] providers = pack.providers;
                    if (providers != null) {
                        for (ProviderInfo provider : providers) {
                            contentProviders.add("content://" + provider.authority);
                        }
                    }
                }
            } catch (Exception e) {
                // PackageManager has died?
                mException = e;
            }
    
            // Sort alphabetically and ignore case sensitivity
            Collections.sort(contentProviders, new Comparator<String>() {
                @Override
                public int compare(String lhs, String rhs) {
                    return lowerCase(lhs).compareTo(lowerCase(rhs));
                }
    
                private String lowerCase(String s) {
                    return s.toLowerCase(Locale.getDefault());
                }
            });
            return contentProviders;
        }
    }
    

    Github: https://github.com/jenzz/ContentProviderHelper

    0 讨论(0)
  • 2020-12-10 06:52

    This is the query to search the bookmarks;

    cursor = mContentResolver.query(Browser.BOOKMARKS_URI, BOOKMARKS_PROJECTION,
                    Browser.BookmarkColumns.BOOKMARK + " == 1" + 
                        " AND LOWER(" + Browser.BookmarkColumns.TITLE + ") LIKE ?", 
                    new String[] { searchText + "%" },
                    Browser.BookmarkColumns.TITLE + " ASC");
    
    0 讨论(0)
提交回复
热议问题