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

如果应用程序已经安装在android中,如何发出警报,android,android-layout,android-intent,android-emulator,android-widget,Android,Android Layout,Android Intent,Android Emulator,Android Widget,如果应用程序已经安装在android中,如何发出警报,如果没有,如何提供到android market的链接??? 我在这里试过了 在这里,我需要退出 public void onClick(View v) { final PackageManager pm = getPackageManager(); //here i get set of installed apps List<ApplicationInfo&

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

在这里,我需要退出

  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);
             }
                    }
              }   
             }
public void onClick(视图v)
{ 
最终PackageManager pm=getPackageManager();
//这里我得到了一套已安装的应用程序
List packages=pm.getinstalledApplication(PackageManager.GET_元数据);
对于(ApplicationInfo packageInfo:packages)
{
//如何使用单击的软件包检查已安装的软件包
字符串数据=packageInfo.packageName;
if(data.equals(“com.bb”))
{
String TAG=“MyActivity”;
Log.d(标签,“已安装的软件包:”+packageInfo.packageName);
Log.d(标签,“启动活动:”+pm.getLaunchIntentForPackage(packageInfo.packageName));
Toast.makeText(Listing.this,“您已经安装了这个包:com.bb”,Toast.LENGTH_SHORT.show();
}
//如果未安装软件包,请执行此操作
其他的
{
字符串字典=项目[位置];
Toast.makeText(Listing.this,dictionary,Toast.LENGTH_SHORT).show();
if(dictionary.equalsIgnoreCase(“首字母缩写”))
{
字符串字典2=“宏名称”;
意向意向=新意向(意向.行动\视图);
intent.setData(Uri.parse(“https://play.google.com/store/apps/details?id=com.“+2”);
星触觉(意向);
}
}
}   
}

问题是,如果条件失败(例如,它进入else块55次)

Try 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(视图v)
{ 
最终PackageManager pm=getPackageManager();
//这里我得到了一套已安装的应用程序
List packages=pm.getinstalledApplication(PackageManager.GET_元数据);
对于(ApplicationInfo packageInfo:packages)
{
//如何使用单击的软件包检查已安装的软件包
字符串数据=packageInfo.packageName;
if(data.equals(“com.bb”))
{
String TAG=“MyActivity”;
Log.d(标签,“已安装的软件包:”+packageInfo.packageName);
Log.d(标签,“启动活动:”+pm.getLaunchIntentForPackage(packageInfo.packageName));
Toast.makeText(Listing.this,“您已经安装了这个包:com.bb”,Toast.LENGTH_SHORT.show();
打破
}否则{//如果未安装软件包,请执行此操作
字符串字典=项目[位置];
Toast.makeText(Listing.this,dictionary,Toast.LENGTH_SHORT).show();
if(dictionary.equalsIgnoreCase(“首字母缩写”))
{
字符串字典2=“宏名称”;
意向意向=新意向(意向.行动\视图);
intent.setData(Uri.parse(“https://play.google.com/store/apps/details?id=com.“+2”);
星触觉(意向);
}
}
}   
}
public void onClick(视图v)
{
字符串str=“aaa”;
如果(包1(str)=1)
{
Toast.makeText(Listing.this,“您已经安装了这个包:aaa”,Toast.LENGTH_SHORT.show();
}
其他的
{
字符串字典2=“bba”;
意向意向=新意向(意向.行动\视图);
intent.setData(Uri.parse(“https://play.google.com/store/apps/details?id=com.“+2”);
星触觉(意向);
}
}
}
公共整数包1(字符串str)
{
int i=0;
最终PackageManager pm=getPackageManager();
//获取已安装应用程序的列表。
List packages=pm.getinstalledApplication(PackageManager.GET_元数据);
对于(ApplicationInfo packageInfo:packages)
{
字符串数据=packageInfo.packageName;
if(数据等于(str))
{
i=1;
}
其他的
{
//i=0;
}
}
返回i;
}

是的,我这样做了,但是,else循环开始运行55次,什么?你确定吗?现在请清理构建。然后尝试更新的答案。或者简单地复制粘贴它。包将包含55个包名…请参阅for循环,它将运行55次,因此else循环将运行。请参阅,这些是java基础知识。如果“If”中的条件为false,则每次进入for循环时,它都会命中else。顺便说一句,if和else不是循环。你可以说它们阻塞。是的,我知道…现在如何检查和迭代循环
     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;

              }