How do I programmatically launch a specific application?

Viewed 73456

I want to launch a specif application.

I know how to do Intents but I want to avoid the selection menu if there are multiple apps that can handle the intent, I want to go directly to a particular app. Hope this makes sense.

12 Answers

in oncreate method call => openApp(); method

private void openApp() {
    String packageName = "com.google.android.gm";
    if (isAppInstalled(activity, packageName))
        startActivity(getPackageManager().getLaunchIntentForPackage(packageName));
    else Toast.makeText(activity, "App not installed", Toast.LENGTH_SHORT).show();
}

public static boolean isAppInstalled(Activity activity, String packageName) {
    PackageManager pm = activity.getPackageManager();
    try {
        pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (PackageManager.NameNotFoundException e) {
    }
    return false;
}

Say you want to open Wonder Clock from your app

URL: https://play.google.com/store/apps/details?id=ganesha.diva.app.wonderclock.free&hl=en_US

id field/package name is of our interest = ganesha.diva.app.wonderclock.free

The Package Manager keeps track of the apps installed on the device and maintains the list of the packages. So anything related to installed apps on the device, the Package manager should be consulted.

The bundle id or package name is unique across globe so it can be used to check the existence of the package/app on the device.

To Open the Wonder Clock from your app

  • Ask Package Manager, for the launcher activity of the given package name

    Intent intent = context.getPackageManager().getLaunchIntentForPackage("ganesha.diva.app.wonderclock.free");

  • Check if launcher activity exists or not if(intent != null) continue... else app is not installed on the device take user to the URL

  • Add the required flags to the intent intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

  • start the activity startActivity(intent);

I've resolved issue by

String packageName = "Your package name";

Intent intent = getPackageManager().getLaunchIntentForPackage(packageName);

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

Launch app using Intent with ComponentName

ComponentName cName = new ComponentName("packageName","packagename.yourMainActivity");
Intent intent = new Intent("android.intent.action.MAIN");
intent.setComponent(cName);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(intent);

This is what I use to open apps

private void launchApp(String pack) {
    Intent launchIntent = new Intent(Intent.ACTION_VIEW);
    launchIntent.setData(Uri.parse("android-app://".concat(pack)));
    startActivity(launchIntent);
}

And this is how to use it

launchApp("your.package.here");

From SDK 30 (Android 11) you need to add to your manifest:

<queries>
    <package android:name="com.app.you.need" />
</queries>

and then this:

    Intent intent = getApplicationContext().getPackageManager().getLaunchIntentForPackage("com.app.you.need");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

Use this method:

Intent oa = new Intent();
oa.setAction(Intent.ACTION_VIEW);
oa.setData(Uri.parse("android-app://com.aksoftlab.application"));
startActivity(oa);
Related