开发者

Get a list of available Content Providers

开发者 https://www.devze.com 2022-12-15 15:52 出处:网络
Is there a way to programmatically list all available content providers on a device?No real use case, I just though开发者_运维知识库t it might be neat to see what apps I have installed on my phone tha

Is there a way to programmatically list all available content providers on a device? No real use case, I just though开发者_运维知识库t it might be neat to see what apps I have installed on my phone that have exposed content providers.


It should be possible by calling PackageManager.getInstalledPackages() with GET_PROVIDERS.

EDIT: example:

    for (PackageInfo pack : getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS)) {
        ProviderInfo[] providers = pack.providers;
        if (providers != null) {
            for (ProviderInfo provider : providers) {
                Log.d("Example", "provider: " + provider.authority);
            }
        }
    }


From the command line, run:

adb shell dumpsys | grep Provider{

Note the opening brace. This will give you a short list of all the providers installed through various packages.


I used adb shell command like this $ adb shell dumpsys > dumpsys.txt and search for content providers string in the output file. From that i can see the list of content providers in the device/emulator.


List<ProviderInfo> providers = getContext().getPackageManager()
    .queryContentProviders(null, 0, 0);

lists all content providers available to you on this device.

Or, if you know the process name and UID of the provider, you can reduce the list by specifying those two parameters. I have used this before to check the existence of my own content providers, more specifically those of previous (free vs. paid) installations:

List<ProviderInfo> providers = getContext().getPackageManager()
    .queryContentProviders("com.mypackage", Process.myUid(), 0);

Note the android.os.Process.myUid() to get my own process's user ID.


The list of registered content providers can be gathered with:

adb shell dumpsys package providers

Tested on Android 8.1 Oreo


List<ProviderInfo> returnList = new ArrayList<ProvderInfo>();
   for (PackageInfo pack:getPackageManager().getInstalledPackages(PackageManager.GET_PROVIDERS)) 
   {
    ProviderInfo[] providers = pack.providers;
   if (providers != null) 
   { 
      returnList.addAll(Arrays.asList(providers)); 
   } 
 } 
 return returnList;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号