【发布时间】:2012-01-27 10:12:35
【问题描述】:
我正在尝试获取给定Intent 的默认/首选应用程序。例如,当用户安装第二个 Web 浏览器,然后尝试打开一个 URL 时,他或她将看到如下对话框:
如果用户随后选择了默认情况下用于此操作选项,则在按下 URL 时对话框将不再打开。
我正在开发一个应该知道这个默认或首选应用程序/操作的应用程序。我该怎么做呢?我目前正在使用下面的代码,但 getPreferredPackage 没有返回任何内容:
Intent i = (new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.google.com"));
PackageManager pm = context.getPackageManager();
final List<ResolveInfo> list = pm.queryIntentActivities(i, 0);
IntentFilter ifilter = new IntentFilter(i.getAction());
if (i.getCategories() != null) {
for(String category : i.getCategories()) {
ifilter.addCategory(category);
}
}
List<IntentFilter> filters = new ArrayList<IntentFilter>();
filters.add(ifilter);
List<ComponentName> preferredActivities = new ArrayList<ComponentName>();
pm.getPreferredActivities(filters, preferredActivities, null);
for (ComponentName activity : preferredActivities) {
for (ResolveInfo rinfo : list) {
if (rinfo.activityInfo.applicationInfo.packageName.equals(activity.getPackageName())) {
try {
final PackageInfo pi = pm.getPackageInfo(activity.getPackageName(), 0);
Toast.makeText(context, pm.getApplicationLabel(pi.applicationInfo), Toast.LENGTH_SHORT).show();
}
} catch (NameNotFoundException e) {
e.printStackTrace();
}
}
}
我做错了什么?这甚至是正确的方法吗?
【问题讨论】:
-
我可能已经找到了解决方案。我正在将resolveinfo与packageinfo进行比较...我会先尝试一下,如果可行,则删除此问题。
-
我没有找到答案,但我确实更改了上面的代码以反映我的发现(但经过测试,它仍然不起作用)。
标签: android android-package-managers