Kalai Selvan.G
Kalai Selvan.G

Reputation: 482

how to give alert if the app is already installed in android

How to give alert if the app is already installed in android,if not how to provide the link to android market???
I tried here

Here in For loop i need to quit

  public void onClick(View v) 
    { 
          final PackageManager pm = getPackageManager();
           //here i get set of installed apps
         List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

      for (ApplicationInfo packageInfo : packages) 
       {
            //how do i check for installed package with clicked package

          String data=packageInfo.packageName;

          if(data.equals("com.bb"))
          {
            String TAG ="MyActivity";
            Log.d(TAG, "Installed package :" + packageInfo.packageName);
            Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
            Toast.makeText(Listing.this, "You have Installed this Package:com.bb" , Toast.LENGTH_SHORT).show();  

          }

                //if the package is not installed, do this

                  else 
          {


            String dictionary=items[position];
            Toast.makeText(Listing.this, dictionary , Toast.LENGTH_SHORT).show();


            if(dictionary.equalsIgnoreCase("Acronyms"))
            {
                String dictionary2="MAcronyms";
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com." + dictionary2));
                startActivity(intent);
             }
                    }
              }   
             }

the problem is loop is still running if, if condition fails(for eg..55 times it enters else Block)

Upvotes: 0

Views: 352

Answers (2)

Kalai Selvan.G
Kalai Selvan.G

Reputation: 482

     public void onClick(View v) 
      {

     String str = "aaa";

     if(package1(str)==1)
     {
         Toast.makeText(Listing.this, "You have Installed this Package:aaa" , Toast.LENGTH_SHORT).show();
     }
     else
     {
            String dictionary2="bba";
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com." + dictionary2));
            startActivity(intent);
     }

  }

     }

public int package1(String str)
{
      int i=0;

      final PackageManager pm = getPackageManager();
        //get a list of installed apps.
      List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

      for (ApplicationInfo packageInfo : packages) 
       {

          String data=packageInfo.packageName;
          if(data.equals(str))
          {
            i=1;

           }
                else
                    {
                   //i=0;

                     }

                 }
                 return i;

              }

Upvotes: 0

Rajkiran
Rajkiran

Reputation: 16193

Try clean->build, and then the following code-

public void onClick(View v) 
{ 
    final PackageManager pm = getPackageManager();
    //here i get set of installed apps
    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);

    for (ApplicationInfo packageInfo : packages) 
    {
        //how do i check for installed package with clicked package

        String data=packageInfo.packageName;

        if(data.equals("com.bb"))
        {
            String TAG ="MyActivity";
            Log.d(TAG, "Installed package :" + packageInfo.packageName);
            Log.d(TAG, "Launch Activity :" + pm.getLaunchIntentForPackage(packageInfo.packageName)); 
            Toast.makeText(Listing.this, "You have Installed this Package:com.bb" , Toast.LENGTH_SHORT).show();  
            break;
        }else{//if the package is not installed, do this


            String dictionary=items[position];
            Toast.makeText(Listing.this, dictionary , Toast.LENGTH_SHORT).show();


            if(dictionary.equalsIgnoreCase("Acronyms"))
            {
                String dictionary2="MAcronyms";
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse("https://play.google.com/store/apps/details?id=com." + dictionary2));
                startActivity(intent);
            }
        }
    }   
}

Upvotes: 1

Related Questions