4

I want to create an activity that launches an external app (shazam for example) on button click. Is it possible? How can it be done?

Thanks!

user2740785
  • 121
  • 1
  • 3
  • 9
  • Do you tried any thing?? look this http://developer.android.com/training/basics/intents/filters.html – Sree Jun 12 '15 at 10:36
  • 1
    possible duplicate of [Launch an application from another application on Android](http://stackoverflow.com/questions/3872063/launch-an-application-from-another-application-on-android) – Sree Jun 12 '15 at 10:38
  • I had given you ans hope it will help you ..check it – Tufan Jun 12 '15 at 10:47

2 Answers2

7

I found the solution. In the manifest file of the application I found My package name: com.package.address and the name of the Mainctivity which I want to launch:

The following code starts this application:

  Intent intent = new Intent(Intent.ACTION_MAIN);
  intent.setComponent(new ComponentName("com.package.address","com.package.address.MainActivity"));
  startActivity(intent);
Tufan
  • 3,168
  • 4
  • 28
  • 52
3

You need other app package name (which can be checked using adb if you have this app installed)

adb shell cmd package list packages | grep shazam

or you can check it in Google Play app page -take look at URL: https://play.google.com/store/apps/details?id=com.shazam.android

Then just use following code (as context you will probably use your activity)

PackageManager pm = context.getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage("com.shazam.android");
    if (intent != null) {
        context.startActivity(intent);
    }

or you can move it (as I would suggest) into separate method, e.g.

    public static void openApp(Context context, String appPackageName) {
    if (context == null) {
        Log.e("<Class name>","Context is null");
        return;
    }
    PackageManager pm = context.getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(appPackageName);
    if (intent != null) {
        context.startActivity(intent);
    }else{
        Log.e("<Class name>", "Cannot start app, appPackageName:'" + appPackageName + "'");
    }
}