6

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
  • 141
  • 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
  • I had given you ans hope it will help you ..check it –  Jun 12 '15 at 10:47

1 Answers1

4

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 + "'");
    }
}