How to check programmatically if an application is installed or not in Android?

后端 未结 15 1283
悲哀的现实
悲哀的现实 2020-11-22 05:51

We have installed applications programmatically.

  1. If the application is already installed in the device the application is open automatically.
  2. Otherwis
15条回答
  •  眼角桃花
    2020-11-22 06:52

    Somewhat cleaner solution than the accepted answer (based on this question):

    public static boolean isAppInstalled(Context context, String packageName) {
        try {
            context.getPackageManager().getApplicationInfo(packageName, 0);
            return true;
        }
        catch (PackageManager.NameNotFoundException e) {
            return false;
        }
    }
    

    I chose to put it in a helper class as a static utility. Usage example:

    boolean whatsappFound = AndroidUtils.isAppInstalled(context, "com.whatsapp");
    

    This answer shows how to get the app from the Play Store if the app is missing, though care needs to be taken on devices that don't have the Play Store.

提交回复
热议问题