Tuesday, August 14, 2012

Check if a app is instlled in Android

In the last two posts Start a specified app and Start a specified app with data passed, it's assumed that the slave app is installed. In order to check if it's installed, we can call PackageManager.queryIntentActivities(). 

 private void startSlave(String passingText){

    Intent intent = new Intent();

    intent.setClassName(PACKAGE_NAME, CLASS_NAME);

    

    Bundle bundle = new Bundle();

    bundle.putString("key", passingText);

       intent.putExtras(bundle);

    

       //check if the slave app installed

       List<ResolveInfo> list = getPackageManager().queryIntentActivities(

         intent,  

               PackageManager.MATCH_DEFAULT_ONLY); 



       if (list.size() >0 ){

        //Yes, the Slave App installed

        startActivity(intent);

       }else{

        //No!

        Toast.makeText(AndroidMasterActivity.this,

          "Slave App not yet installed!",

          Toast.LENGTH_LONG).show();

       } 

   }

No comments:

Post a Comment