1

I have developed two apps. In one I am showing videos from youtube. In the second, I display news and I have a menu in that which have to open the first app.

I know to open the second app with an intent, but I couldn't find a way to check whether the app is installed or not.

How can I check whether the app is installed, and open it if is and open playstore page of the app if it is not currently installed.

Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
  • 1
    Possible duplicate of [How to check programmatically if an application is installed or not in Android?](http://stackoverflow.com/questions/11392183/how-to-check-programmatically-if-an-application-is-installed-or-not-in-android) – ΦXocę 웃 Пepeúpa ツ Mar 30 '16 at 18:11
  • Possible duplicate of [How to check programatically if an App is installed?](http://stackoverflow.com/questions/3512228/how-to-check-programatically-if-an-app-is-installed) – Arun Shankar Mar 30 '16 at 18:11

2 Answers2

1

Use this method.

public void OpenApp() {
    PackageManager pm = getPackageManager();
    final String LiveAppPackage = "com.example.app"; //Change to your package name.
    try {
        pm.getPackageInfo(LiveAppPackage, PackageManager.GET_ACTIVITIES);
        Intent intent = pm.getLaunchIntentForPackage(LiveAppPackage);
            if (intent == null) {
                throw new PackageManager.NameNotFoundException();
            }
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            context.startActivity(intent);

    }
    catch (PackageManager.NameNotFoundException e) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                ActivityThis);

        DialogInterface.OnClickListener onClickListener =  new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        if(which == DialogInterface.BUTTON_POSITIVE){
                                //Try to open Market and if fails, open play store.
                                try {
                                    startActivity(new Intent(Intent.ACTION_VIEW, Uri
                                            .parse("market://details?id="
                                                    + LiveAppPackage)));
                                } catch (android.content.ActivityNotFoundException e1) {
                                    startActivity(new Intent(
                                            Intent.ACTION_VIEW,
                                            Uri.parse("https://play.google.com/store/apps/details?id="
                                                    + LiveAppPackage)));
                                }

                        } else{
                            dialog.dismiss();
                        }
                    }
                };

        alertDialogBuilder.setTitle("App Title")
                .setCancelable(true)
                .setPositiveButton("Install", onClickListener);
        alertDialogBuilder.setNegativeButton("Cancel", onClickListener);

        alertDialogBuilder.setMessage("Second app is not currently installed\n\nLike to install it?"); //Change to your message

        // create alert dialog
        AlertDialog alertDialog = alertDialogBuilder.create();

        // show it
        alertDialog.show();
    }
}
Arun Shankar
  • 2,603
  • 2
  • 26
  • 36
0

To know if the app is installed or not, you can use PackageManager like this :-

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

To use this method, you need to pass the package name of your second app. I suppose you know the packageName. Rest is cakewalk. :)

appInstalledOrNot("com.dexter.lab");
Dave Ranjan
  • 2,966
  • 24
  • 55