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

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

    This code checks to make sure the app is installed, but also checks to make sure it's enabled.

    private boolean isAppInstalled(String packageName) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(packageName, PackageManager.GET_ACTIVITIES);
            return pm.getApplicationInfo(packageName, 0).enabled;
        }
        catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 06:53

    Try with this:

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            // Add respective layout
            setContentView(R.layout.main_activity);
    
            // Use package name which we want to check
            boolean isAppInstalled = appInstalledOrNot("com.check.application");  
    
            if(isAppInstalled) {
                //This intent will help you to launch if the package is already installed
                Intent LaunchIntent = getPackageManager()
                    .getLaunchIntentForPackage("com.check.application");
                startActivity(LaunchIntent);
    
                Log.i("Application is already installed.");       
            } else {
                // Do whatever we want to do if application not installed
                // For example, Redirect to play store
    
                Log.i("Application is not currently installed.");
            }
        }
    
        private boolean appInstalledOrNot(String uri) {
            PackageManager pm = getPackageManager();
            try {
                pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
                return true;
            } catch (PackageManager.NameNotFoundException e) {
            }
    
            return false;
        }
    
    }
    
    0 讨论(0)
  • 2020-11-22 06:54

    A cool answer to other problems. If you do not want to differentiate "com.myapp.debug" and "com.myapp.release" for example !

    public static boolean isAppInstalled(final Context context, final String packageName) {
        final List<ApplicationInfo> appsInfo = context.getPackageManager().getInstalledApplications(0);
        for (final ApplicationInfo appInfo : appsInfo) {
            if (appInfo.packageName.contains(packageName)) return true;
        }
        return false;
    }
    
    0 讨论(0)
提交回复
热议问题