如果应用程序已安装在android中,如何提醒

时间:2012-03-21 08:24:31

标签: android android-layout android-intent android-emulator android-widget

如果应用程序已安装在android中,如何不提供如何提供android市场的链接?如何提醒 我试过这里

这里在For循环中我需要退出

  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);
             }
                    }
              }   
             }

问题是循环仍在运行,如果条件失败(例如......55次进入其他区块)

2 个答案:

答案 0 :(得分:1)

尝试clean-&gt; build,然后使用以下代码 -

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);
            }
        }
    }   
}

答案 1 :(得分:0)

     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;

              }