我做什么
我正在检索所有已安装应用的列表,例如以下内容(适用于类似应用的启动器):
PackageManager pm = context.getPackageManager();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER);
// flag PackageManager.GET_RESOLVED_FILTER will set ResolveInfo.filter (not documented, but tested)
// many examples use flag == 0, but then the filter is always null
List<ResolveInfo> ril = pm.queryIntentActivities(mainIntent, PackageManager.GET_RESOLVED_FILTER);
if (ril != null) {
for (ResolveInfo ri : ril) {
// I need this info!
// ri.isDefault is always false...
boolean isDefault = ri.isDefault || ril.filter.hasCategory(Intent.CATEGORY_DEFAULT);
}
}
到目前为止,这是有效的。但是像samsungs手机应用程序这样的应用程序不尊重他们应该只用Intent.CATEGORY_DEFAULT
标记一个活动。以下是三星应用程序中两项活动的类别:
ActivityInfo{cd007eb com.android.contacts.activities.PeopleActivity}
filter:
0 = "android.intent.category.DEFAULT"
1 = "android.intent.category.LAUNCHER"
2 = "android.intent.category.BROWSABLE"
3 = "android.intent.category.APP_CONTACTS"
ActivityInfo{3e9ba8d com.android.dialer.DialtactsActivity}
filter:
0 = "android.intent.category.DEFAULT"
1 = "android.intent.category.LAUNCHER"
2 = "android.intent.category.BROWSABLE"
要求
ResolveInfo
是启动的那个,我需要信息,就像我使用以下命令检索到的意图一样:pm.getLaunchIntentForPackage(packageName)
。这就是我在本文中称之为默认启动意图的内容问题
如果ResolveInfo
属于默认启动意图?如何可靠地找出?
答案 0 :(得分:0)
<强>解强>
只要您假设只有一些应用注册了多个默认活动,此解决方案就可以使用。
ResolveInfos
HashMap
并在列表HashMap
确定您是否需要检查ResolveInfo
是否属于默认活动ResolveInfo
<强>代码强>
List<ResolveInfo> ril = ...;
HashMap<String, Integer> packageCountMap = getPackageCountMap(uniqueRil);
for (ResolveInfo ri :: ril) {
boolean isDefault = isDefaultIntentForPhoneApp(packageCountMap, ri);
// use this info...
}
助手功能:
public static HashMap<String, Integer> getPackageCountMap(List<ResolveInfo> ril) {
HashMap<String, Integer> map = new HashMap<>();
ResolveInfo ri;
Integer count;
if (ril != null) {
for (int i = 0; i < ril.size(); i++) {
ri = ril.get(i);
count = map.get(ri.activityInfo.packageName);
if (count == null) {
count = 1;
} else {
count++;
}
map.put(ri.activityInfo.packageName, count);
}
}
return map;
}
public static boolean isDefaultIntentForPhoneApp(PackageManager pm, HashMap<String, Integer> packageCountMap, ResolveInfo ri) {
boolean isDefault;
// count how often the package name exists => 1x => then the activity must be the default one, no need to do any check
Integer count = packageCountMap.get(ri.activityInfo.packageName);
if (count == null || count <= 1) {
isDefault = true;
}
// otherwise get default activity and compare it
else {
Intent i = pm.getLaunchIntentForPackage(packageName);
ComponentName cn = i.resolveActivity(pm);
isDefault = cn != null && cn.getClassName().equals(ri.activityInfo.name);
}
return isDefault;
}
答案 1 :(得分:0)
您可以按如下方式使用queryIntentActivities
:
PackageManager pm = context.getPackageManager();
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null)
.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> ril = pm.queryIntentActivities(mainIntent, PackageManager.MATCH_DEFAULT_ONLY);
基于 MATCH_DEFAULT_ONLY 的文档:
分辨率和查询标志:如果设置,只有支持的过滤器 CATEGORY_DEFAULT将被考虑进行匹配。这是一个同义词 在您提供的Intent中包含CATEGORY_DEFAULT。