Get intent filter for receivers

2019-02-06 13:50发布

问题:

I am trying to get a list of receivers that handle android.intent.action.BOOT_COMPLETED for other applications.

I can get only the apps with that action with this:

final PackageManager pm = getPackageManager();
final Intent intent = new Intent("android.intent.action.BOOT_COMPLETED");
final List<ResolveInfo> activities = pm.queryBroadcastReceivers(intent, 0);
for (ResolveInfo ri : activities) {
    Log.i(TAG, "app name: " + ri.loadLabel(pm));
}

I can get a list of receivers with this code:

final List<PackageInfo> packs = pm.getInstalledPackages(PackageManager.GET_RECEIVERS);
for (final PackageInfo p : packs) {
    ActivityInfo[] receivers = p.receivers;
    if (receivers != null) {
        for (ActivityInfo ai : receivers) {
            Log.i(TAG, "receiver: " + ai.name);
        }
    }                   
}

After I get the receivers I cannot tell what the intent-filter/action is. If an app has two receivers I need to know which one handles android.intent.action.BOOT_COMPLETED. Is there a way I can get this info?

回答1:

I reformatted for people. Use this and thanks for question.

PackageManager packageManager = getPackageManager();
List<String> startupApps = new ArrayList<String>();
Intent intent = new Intent(Intent.ACTION_BOOT_COMPLETED);
List<ResolveInfo> activities = packageManager.queryBroadcastReceivers(intent, 0);
for (ResolveInfo resolveInfo : activities) {
    ActivityInfo activityInfo = resolveInfo.activityInfo;
    if (activityInfo != null) {
        startupApps.add(activityInfo.name);
    }
}