start application knowing package name

Android

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

Try using PackageManager and getLaunchIntentForPackage()

Solution 3 - Android

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();
}

Solution 4 - Android

Intent intent;                                        
PackageManager pm = getActivity().getPackageManager();

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

startActivity(intent);

Solution 5 - Android

    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();
    }

Solution 6 - Android

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbartView Question on Stackoverflow
Solution 1 - Androidpatric_cenaView Answer on Stackoverflow
Solution 2 - AndroidCommonsWareView Answer on Stackoverflow
Solution 3 - AndroidAnubian NoobView Answer on Stackoverflow
Solution 4 - AndroidSiddhi MandodiView Answer on Stackoverflow
Solution 5 - AndroidPratik FagadiyaView Answer on Stackoverflow
Solution 6 - AndroidChethana ArunodhView Answer on Stackoverflow