【问题标题】:how to give alert if the app is already installed in android如果应用程序已安装在android中,如何发出警报
【发布时间】:2012-03-21 08:24:30
【问题描述】:

如果应用程序已经安装在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);
             }
                    }
              }   
             }

问题是如果条件失败,则循环仍在运行(例如,它进入 else 块的 55 次)

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 是的,我这样做了,但是,否则循环开始运行 55 次,伙计
    • 什么?你确定吗?现在请清理构建。并尝试更新的答案。或者直接复制粘贴。
    • 包将包含 55 个包名...查看 for 循环它将运行 55 次,因此 else 循环执行..
    • 看,这些是java基础。如果“if”中的条件为假,则每次进入 for 循环时都会触发 else。顺便说一句,if 和 else 不是循环。你可能会说他们阻止了。
    • 是的,我知道...现在如何检查和迭代循环朋友
    【解决方案2】:
         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;
    
                  }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      相关资源
      最近更新 更多