How can I find all the packages with a particular intent filter?

旧巷老猫 提交于 2019-12-24 14:43:15

问题


I need to get the names of all the installed packages whose names being with com.mridang. In order to do this, I'm using something on the lines of the following snippet:

for (PackageInfo pkgPackage : mgrPackages.getInstalledPackages(0)) {
    if (pkgPackage.applicationInfo.packageName.startsWith("com.mridang.")) {
        System.out.println(pkgPackage.applicationInfo.packageName);
    }
}

I need to only get the names of those packages which have a service containing the following intent filter com.google.android.apps.dashclock.Extension.

Here's a snippet of of the manifest files of on the example packages:

    <service
        android:name="com.mridang.storage.StorageWidget"
        android:icon="@drawable/ic_dashclock"
        android:label="@string/extension_name"
        android:permission="com.google.android.apps.dashclock.permission.READ_EXTENSION_DATA" >
        <intent-filter>
            <action android:name="com.google.android.apps.dashclock.Extension" />
        </intent-filter>

    </service>

How can I do this?


回答1:


You can do it with PackageManager#queryIntentServices

    Intent intent = new Intent("com.google.android.apps.dashclock.Extension");
    for (ResolveInfo info : mgrPackages.queryIntentServices(intent, 0)) {
        String packageName = info.serviceInfo.applicationInfo.packageName;
        // you can then filter by packageName.startsWith("com.mridang");
        Log.d("Packages", "package = " + packageName);
    }

Hope that helps



来源:https://stackoverflow.com/questions/20846972/how-can-i-find-all-the-packages-with-a-particular-intent-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!