我想列出应用程序(具有相同的 Intent 过滤器)。我能够通过向 Activity 添加 Intent 过滤器来实现这一点
<activity
android:name=".Activities.MainActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateHidden|adjustNothing">
<intent-filter>
<action android:name="com.example.identifier" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="isApp" />
</intent-filter>
</activity>
我可以通过
检索所有具有此 Intent 的应用 String uri = "isApp:";
Intent intent = new Intent("com.example.identifier",
Uri.parse(uri));
PackageManager manager = getPackageManager();
List<ResolveInfo> list = manager.queryIntentActivities(intent, 0);
但是,这会在 intentChoose 中使用此代码段显示时启动 Activity :
Intent zoneIntent = new Intent("com.example.identifier",
Uri.parse(uri));
Intent openInChooser = Intent.createChooser(zoneIntent, "Complete Action with").setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(openInChooser);
但我希望它能调用广播接收器。所以,我将 Intent 移动到 AndroidManifest.xml 中的广播接收器,例如:
<receiver
android:name=".ExampleReceiver"
android:enabled="true">
<intent-filter>
<action android:name="com.example.identifier" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="isApp" />
</intent-filter>
</receiver>
并且返回具有此 Intent 的应用程序数量的代码段现在返回 0,即使该应用程序仍在该设备上也是如此。这可以用广播接收器来完成,还是我应该考虑用另一种方法。谢谢。
最佳答案
调用queryIntentActivities()
只会返回Activity
。它不会返回 BroadcastReceiver
。如果您想使用 BroadcastReceiver
执行此操作,则需要调用 queryBroadcastReceivers()
。
关于android - 无法在广播接收器中使用 Intent 过滤器列出应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33211432/