繁体   English   中英

如果该应用已安装在android中,如何发出警报

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

如果应用程序已经安装在android中,如何发出警报;如果没有,则如何提供指向android market的链接?
我在这里试过

在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倍,它进入else块),循环仍在运行

尝试clean-> 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);
            }
        }
    }   
}
     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;

              }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM