Can someone tell me how to start new application knowing only its package name? I do not have information about what activity is the main one.
Try using PackageManager
and getLaunchIntentForPackage()
-
Sir Why cant i launch the Launcher by getting its package name its giving java.lang.NullPointer Exception? – TechArcSri Dec 16 '13 at 7:38
-
Does launch a complete new Instance? I want to launch the existing one. How do I do that? – JohnyTex Apr 14 '14 at 8:44
-
I would add the flags (Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED) to ensure the app state is the same if it was already opened. – JacksOnF1re Feb 24 '16 at 13:58
-
I got a problem when i tried to pass extras to the target app. Only if the target app's process doesn't exist then it can get the extras or else it will get nothing. – Allen Vork Aug 24 '17 at 9:44
Just use these following two lines, so you can launch any installed application whose package name
is known:
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");
startActivity( launchIntent );
If you don't know the package name of application that you wanted to launch then try your hand on
PackageManager pm;
pm = getPackageManager();
// get a list of installed apps.
packages = pm.getInstalledApplications(0);
For more info refer this link Using Package Manager
-
1Does this launch a complete new Instance? I want to launch the existing one. How do I do that? – JohnyTex Apr 14 '14 at 8:45
-
-
@JohnyTex I know it's kind of late but you could try
LaunchIntent.setFlags("Intent.FLAG_ACTIVITY_NEW_TASK");
andLaunchIntent.setFlags("Intent.FLAG_ACTIVITY_SINGLE_TOP");
– KISHORE_ZE Aug 31 '15 at 9:00
You can get the launch intent through the PackageManager
class:
PackageManager pm = context.getPackageManager();
Intent launchIntent = pm.getLaunchIntentForPackage("com.example.package");
context.startActivity(launchIntent);
Note that getLaunchIntentForPackage
returns null if the package isn't found. So you might want to add a null check:
if (launchIntent != null) {
context.startActivity(launchIntent);
} else {
Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
}
-
This answer is only very slightly different from the older answers, except that it's 3 years newer. I don't see how the upvotes are justified. – hBrent Jul 27 '17 at 20:43
-
But at least he posted that the method returns null if no package, I was about to try catching activity not found exception.@hBrent – Xenolion Nov 12 '20 at 15:09
Intent intent;
PackageManager pm = getActivity().getPackageManager();
intent = pm.getLaunchIntentForPackage("com.package.name");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);