3

I try to create an application that can start other applications (f.e. Gmail or Facebook or any installed one).

I tried to use the following code:

PackageManager pm = MainActivity.this.getPackageManager();
try
{
Intent it = pm.getLaunchIntentForPackage("FULLY QUALIFIED NAME");
if (null != it)
MainActivity.this.startActivity(it);
}
catch (ActivityNotFoundException e)
{ }

However, it requires the fully qualified name of the applications.

How can I acquire it? Is there any build in method to get them?

4

2 回答 2

2

一个应用程序可能有零个、一个或多个属于启动器的活动。因此,启动器不应该问“所有应用程序是什么Intent,每个应用程序的启动是什么?” 相反,启动器应该问“我应该展示哪些活动?”

这是使用PackageManager和完成的queryIntentActivities()这个示例项目实现了一个完整的启动器。关键线路是:

PackageManager pm=getPackageManager();
Intent main=new Intent(Intent.ACTION_MAIN, null);

main.addCategory(Intent.CATEGORY_LAUNCHER);

List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);

然后,您可以使用任何您想要呈现该launchables集合的机制。示例项目将它们放在一个ListView.

于 2014-01-31T22:12:02.293 回答
1

您可以获得所有应用程序的列表,如下所示:

final PackageManager packageManager = getPackageManager();
List<ApplicationInfo> packages = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);

现在您已将所有应用程序及其元数据存储在List. 你可以像这样得到他们的包名:

for (ApplicationInfo packageInfo : packages) {
    Log.d(TAG, packageManager.getLaunchIntentForPackage(packageInfo.packageName)); 
}
于 2014-01-31T22:02:39.833 回答