Android 获取可用内容提供者的列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2001590/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get a list of available Content Providers
提问by Mark B
Is there a way to programmatically list all available content providerson a device? No real use case, I just thought it might be neat to see what apps I have installed on my phone that have exposed content providers.
有没有办法以编程方式列出设备上所有可用的内容提供者?没有真正的用例,我只是想看看我在手机上安装了哪些暴露了内容提供者的应用程序可能很不错。
回答by Mirko N.
It should be possible by calling PackageManager.getInstalledPackages()with GET_PROVIDERS
.
应该通过调用可能PackageManager.getInstalledPackages()用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);
}
}
}
回答by Simon Guest
From the command line, run:
从命令行运行:
adb shell dumpsys | grep Provider{
adb shell dumpsys | grep Provider{
Note the opening brace. This will give you a short list of all the providers installed through various packages.
注意左括号。这将为您提供通过各种软件包安装的所有提供程序的简短列表。
回答by Tuyen Dinh
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.
我使用了这样的 adb shell 命令$ adb shell dumpsys > dumpsys.txt
并在输出文件中搜索内容提供程序字符串。从中我可以看到设备/模拟器中的内容提供者列表。
回答by Cornel Masson
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 owncontent providers, more specifically those of previous (free vs. paid) installations:
或者,如果您知道提供程序的进程名称和 UID,您可以通过指定这两个参数来减少列表。我之前使用过它来检查我自己的内容提供商是否存在,更具体地说是以前(免费与付费)安装的内容提供商:
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.
请注意android.os.Process.myUid()
获取我自己的进程的用户 ID。
回答by manDroid
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;
回答by Yom
The list of registered content providers can be gathered with:
可以通过以下方式收集已注册的内容提供商列表:
adb shell dumpsys package providers
adb shell dumpsys package providers
Tested on Android 8.1 Oreo
在 Android 8.1 Oreo 上测试