46

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.

Zoe
  • 27,060
  • 21
  • 118
  • 148
mbwasi
  • 3,612
  • 3
  • 30
  • 36
  • 2
    possible duplicate of [Open another one application from our application?](http://stackoverflow.com/questions/3342655/open-another-one-application-from-our-application) – Pentium10 Jul 27 '10 at 12:19

12 Answers12

74

You should use the function of the package manager.

Context ctx=this; // or you can replace **'this'** with your **ActivityName.this**
try {
Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
ctx.startActivity(i);
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
}
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
Christopher Gertz
  • 1,826
  • 1
  • 15
  • 9
  • 2
    What is ctx ? object or keyword ? – Azahar Jan 28 '14 at 07:27
  • 3
    It's an [Android Context object](http://developer.android.com/reference/android/content/Context.html), e.g. an Activity or the return value of getApplicationContext(). – Christopher Gertz Feb 02 '14 at 12:14
  • It is important: You can use getApplicationContext only when you are in a Activity Class man. – Franklin Hirata Feb 25 '14 at 13:39
  • 1
    Not entirely true. It works with all descendants of ContextWrapper. Additional important examples are the Application class and the Service class. Anyway, on views you can use getContext(), in Fragments you can use getActivity(). Usually it's no problem to obtain a valid Context object... That's why I did not explicitly define it in my original answer. – Christopher Gertz Mar 05 '14 at 13:02
  • best answer for all scenarios – iBabur Nov 12 '14 at 19:25
  • Why is this method better than the accepted answer? – Mickäel A. Dec 09 '20 at 10:57
40

You use the package name / class directly, for example to create a new intent to call the twidroid program you'd use the followinglink text:

 Intent intent = new Intent("com.twidroid.SendTweet");

You'd probably want to put a try/catch around for a ActivityNotFoundException for when the application is not installed.

stealthcopter
  • 13,964
  • 13
  • 65
  • 83
  • 8
    If it happens, you can throw a exception and open application on GooglePlay. ;) – Franklin Hirata Feb 25 '14 at 13:40
  • On thing to consider in the latest versions of android is the package visibility filter https://docs.google.com/spreadsheets/d/1t-NFOyeohar1WbukAx3rYExS3MhgAMJ6E5enVssma_Q/edit#gid=0, you need to update your AndroidManifest – Calin Sep 14 '22 at 07:30
12
Intent intent = new Intent();    
intent.setClassName("package.name", "package.name.LauncherActivityName");
startActivityForResult(intent,REQUEST_CODE);
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
Bharat Dodeja
  • 1,137
  • 16
  • 22
5

I use:

  try {
       Intent intent = new Intent();    
       intent.setClassName("package.name", "<your_package_name>");
       startActivity(intent);
    } catch (NameNotFoundException e) {
       Log.e(TAG, e.getMessage());
    }

But like Cami suggested this will work too:

try {
    Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.twidroid.SendTweet");
    ctx.startActivity(i);
} catch (NameNotFoundException e) {
     Log.e(TAG, e.getMessage());
}
Jorgesys
  • 124,308
  • 23
  • 334
  • 268
5

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);
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Fabio Fracassi
  • 289
  • 3
  • 7
4

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;
}
Gowthaman M
  • 8,057
  • 8
  • 35
  • 54
Ahamadullah Saikat
  • 4,437
  • 42
  • 39
4

The activity you are calling should appear not only in the Manifest for its own package, but in the Manifest for the CALLING package, too. - don't forget!

Gangnus
  • 24,044
  • 16
  • 90
  • 149
3

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

Community
  • 1
  • 1
Devendra Vaja
  • 3,836
  • 2
  • 19
  • 14
3

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);
Waqar UlHaq
  • 6,144
  • 2
  • 34
  • 42
3

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");
BOT Panzer
  • 51
  • 5
2

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);
Benny
  • 2,233
  • 1
  • 22
  • 27
1

Use this method:

Intent oa = new Intent();
oa.setAction(Intent.ACTION_VIEW);
oa.setData(Uri.parse("android-app://com.aksoftlab.application"));
startActivity(oa);
tripleee
  • 175,061
  • 34
  • 275
  • 318
AkSoftLab
  • 11
  • 2