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

后端 未结 15 1067
悲哀的现实
悲哀的现实 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:40

    I think using try/catch pattern is not very well for performance. I advice to use this:

    public static boolean appInstalledOrNot(Context context, String uri) {
        PackageManager pm = context.getPackageManager();
        List packageInfoList = pm.getInstalledPackages(PackageManager.GET_ACTIVITIES);
        if (packageInfoList != null) {
            for (PackageInfo packageInfo : packageInfoList) {
                String packageName = packageInfo.packageName;
                if (packageName != null && packageName.equals(uri)) {
                    return true;
                }
            }
        }
        return false;
    }
    

提交回复
热议问题