86

How can i start a new application using its package name? I don't have information about what activity is the main one.

TheGoldenTree
  • 158
  • 4
  • 16
bart
  • 2,378
  • 2
  • 19
  • 21

6 Answers6

166

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 information, refer to this link: Using Package Manager.

BSMP
  • 4,596
  • 8
  • 33
  • 44
patric_cena
  • 1,986
  • 1
  • 12
  • 17
104

Try using PackageManager and getLaunchIntentForPackage()

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Sir Why cant i launch the Launcher by getting its package name its giving java.lang.NullPointer Exception? – TechArcSri Dec 16 '13 at 07:38
  • Does launch a complete new Instance? I want to launch the existing one. How do I do that? – JohnyTex Apr 14 '14 at 08: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 09:44
  • 1
    Is this still a reliable solution after Package Visibility changes on Android 11? – argenkiwi Apr 28 '21 at 21:29
  • 1
    @argenkiwi: I very rarely use this method. I expect that either it will work "out of the box", or you would need `` for a `MAIN`/`LAUNCHER` `Intent` structure. AFAIK, it is not completely broken. – CommonsWare Apr 28 '21 at 22:02
  • @argenkiwi you mean android 12? – Nick May 11 '21 at 01:25
  • No, I meant Android 11 as per this section of the docs: https://developer.android.com/about/versions/11/privacy/package-visibility – argenkiwi May 13 '21 at 01:24
23

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();
}
Anubian Noob
  • 13,426
  • 6
  • 53
  • 75
  • 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
3
Intent intent;                                        
PackageManager pm = getActivity().getPackageManager();

intent = pm.getLaunchIntentForPackage("com.package.name");                       
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);
Vidya L
  • 2,289
  • 4
  • 27
  • 42
2
    Intent intent = getPackageManager().getLaunchIntentForPackage("app.package.name");
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    if (intent != null) {
        startActivity(intent);
    } else {
        Toast.makeText(context, "Package not found", Toast.LENGTH_SHORT).show();
    }
Dharman
  • 30,962
  • 25
  • 85
  • 135
Pratik Fagadiya
  • 1,195
  • 7
  • 21
2
val packageName = "com.google.android.youtube"
var intent = activity!!.packageManager.getLaunchIntentForPackage(packageName)

          if (intent == null) {
            if (intent == null) {
                    intent = try {
                        Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName"))
                    } catch (e: Exception) {
                        Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=$packageName"))
                    }
                }
             startActivity(intent)

For Android 11 (API level 30) or higher, in AndroidManifest.xml,

<queries>
    <package android:name="com.google.android.youtube" />
    <package android:name="com.example.app" />
</queries>

Or simply we can allow for all packages (not recommended)

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" tools:ignore="QueryAllPackagesPermission" />

References

Package visibility filtering on Android

Chethana Arunodh
  • 335
  • 3
  • 15