bart
bart

Reputation: 2408

start application knowing package name

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

Upvotes: 89

Views: 109663

Answers (7)

Mughil
Mughil

Reputation: 715

Jetpack Compose

get context within @Composable

val context = LocalContext.current

val launchIntent: Intent? = context.packageManager.getLaunchIntentForPackage(packageId)
            
if (launchIntent != null) {    
startActivity(context, launchIntent, null) }

Upvotes: 0

Chethana Arunodh
Chethana Arunodh

Reputation: 345

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

Upvotes: 2

Pratik Fagadiya
Pratik Fagadiya

Reputation: 1450

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

Upvotes: 2

patric_cena
patric_cena

Reputation: 2006

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.

Upvotes: 168

Siddhi Mandodi
Siddhi Mandodi

Reputation: 31

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

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

startActivity(intent);

Upvotes: 3

Anubian Noob
Anubian Noob

Reputation: 13596

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

Upvotes: 23

CommonsWare
CommonsWare

Reputation: 1006564

Try using PackageManager and getLaunchIntentForPackage()

Upvotes: 104

Related Questions