user2713459
user2713459

Reputation: 49

Launch app using package name

I am trying to launch an application from my own application in Android. What i have done is get all the package names of the apps installed into the device and stored them into a String Array. Then with a list view i try to launch every app selected.

In order to create a new intent to launch every app i use the following code claimed to work in several posts in android tutorial sites:

Intent intent = packageManager.getLaunchIntentForPackage(packages[arg2]);

where packages is the name of my table containing the package names and arg2 the listview selected item.

The problem is that even if i check with toast messages i can see the package name is correct but the getLaunchIntentForPackage is always null and the apps are not starting.

Thanks

Upvotes: 1

Views: 7415

Answers (2)

markas
markas

Reputation: 91

Try this :

String pack[] = {"com.android.setting", "com.android.browser"};     
try {
    Intent intent = getPackageManager().getLaunchIntentForPackage(pack[0]);
    startActivity(intent);
} catch (Exception e) {
    // returns null if application is not installed
}

Upvotes: 1

Anderson K
Anderson K

Reputation: 5505

Try adding the intent

final Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Upvotes: 4

Related Questions