本周早些时候,我问了一个类似的问题,但我仍然不明白如何获得所有已安装的应用程序的列表,然后选择一个运行。
我试过了:
Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);
这只显示预安装或可以运行ACTION_MAIN Intent类型的应用程序。
我也知道我可以使用PackageManager来获取所有已安装的应用程序,但我如何使用它来运行特定的应用程序呢?
本周早些时候,我问了一个类似的问题,但我仍然不明白如何获得所有已安装的应用程序的列表,然后选择一个运行。
我试过了:
Intent intent = new Intent(ACTION_MAIN);
intent.addCategory(CATEGORY_LAUNCHER);
这只显示预安装或可以运行ACTION_MAIN Intent类型的应用程序。
我也知道我可以使用PackageManager来获取所有已安装的应用程序,但我如何使用它来运行特定的应用程序呢?
当前回答
context.getPackageManager () .getInstalledApplications (PackageManager.GET_META_DATA); 应该返回所有已安装应用程序的列表,但在android 11中,它只返回系统应用程序的列表。要获得所有应用程序(系统+用户)的列表,我们需要为应用程序提供额外的权限
< uses-permission android: name = " android.permission.QUERY_ALL_PACKAGES " / >
其他回答
@Jas: 我没有那个代码了,但我找到了一些接近的东西。我已经做了这个来搜索我的应用程序的“组件”,它们只是具有给定类别的活动。
private List<String> getInstalledComponentList() {
Intent componentSearchIntent = new Intent();
componentSearchIntent.addCategory(Constants.COMPONENTS_INTENT_CATEGORY);
componentSearchIntent.setAction(Constants.COMPONENTS_INTENT_ACTION_DEFAULT);
List<ResolveInfo> ril = getPackageManager().queryIntentActivities(componentSearchIntent, PackageManager.MATCH_DEFAULT_ONLY);
List<String> componentList = new ArrayList<String>();
Log.d(LOG_TAG, "Search for installed components found " + ril.size() + " matches.");
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
componentList.add(ri.activityInfo.packageName);// + ri.activityInfo.name);
Log.d(LOG_TAG, "Found installed: " + componentList.get(componentList.size()-1));
}
}
return componentList;
}
我已经注释了它获取活动名称的部分,但它非常简单。
过滤基于系统的应用程序:
private boolean isSystemPackage(ResolveInfo ri) {
return (ri.activityInfo.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0;
}
如果在一个包中有多个启动器,上述代码就有问题。例如:在LG Optimus Facebook为LG, MySpace为LG, Twitter为LG包含在一个包的名称SNS,如果你使用上述SNS将重复。经过几个小时的研究,我得到了以下代码。看起来效果不错。
private List<String> getInstalledComponentList()
throws NameNotFoundException {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> ril = getPackageManager().queryIntentActivities(mainIntent, 0);
List<String> componentList = new ArrayList<String>();
String name = null;
for (ResolveInfo ri : ril) {
if (ri.activityInfo != null) {
Resources res = getPackageManager().getResourcesForApplication(ri.activityInfo.applicationInfo);
if (ri.activityInfo.labelRes != 0) {
name = res.getString(ri.activityInfo.labelRes);
} else {
name = ri.activityInfo.applicationInfo.loadLabel(
getPackageManager()).toString();
}
componentList.add(name);
}
}
return componentList;
}
你可以用这个:
PackageManager pm = getApplicationContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("facebook"))
{
// facebook
}
if ((app.activityInfo.name).contains("android.gm"))
{
// gmail
}
if ((app.activityInfo.name).contains("mms"))
{
// android messaging app
}
if ((app.activityInfo.name).contains("com.android.bluetooth"))
{
// android bluetooth
}
}
自Android 11 (API级别30)以来,大多数用户安装的应用默认是不可见的。你必须静态地声明哪些应用程序和/或意图过滤器你将在你的manifest中获得信息,就像这样:
<manifest>
<queries>
<!-- Explicit apps you know in advance about: -->
<package android:name="com.example.this.app"/>
<package android:name="com.example.this.other.app"/>
<!-- Intent filter signatures that you are going to query: -->
<intent>
<action android:name="android.intent.action.SEND" />
<data android:mimeType="image/jpeg" />
</intent>
</queries>
...
</manifest>
或者要求QUERY_ALL_PACKAGES权限。
在做了上面的事情之后,这里的其他答案仍然适用。
点击这里了解更多信息:
迁移 文档 专用文档页面(有更多 信息) 官方博客 帖子