Pregunta

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?

¿Fue útil?

Solución

An application may have zero, one, or several activities that belong in a launcher. Hence, a launcher should not be asking "what are all the applications, and what is the launch Intent for each?" A launcher should, instead, be asking "what are all of the activities that I should show?"

That is accomplished using PackageManager and queryIntentActivities(). This sample project implements a complete launcher. The key lines are:

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

main.addCategory(Intent.CATEGORY_LAUNCHER);

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

Then, you can use whatever mechanism you want to render that launchables collection. The sample project puts them in a ListView.

Otros consejos

You can get a List of all applications like this:

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

Now you've stored all applications with their meta data in a List. You can get their package name like this:

for (ApplicationInfo packageInfo : packages) {
    Log.d(TAG, packageManager.getLaunchIntentForPackage(packageInfo.packageName)); 
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top