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

后端 未结 15 1307
悲哀的现实
悲哀的现实 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条回答
  •  闹比i
    闹比i (楼主)
    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;
        }
    
    }
    

提交回复
热议问题