Java – why did my string to string comparison fail?

I have an Android application. I want to check whether the installed application name matches the string passed to the function containing this code The codes and examples are as follows:

private Boolean checkInstalledApp(String appName){
    PackageManager pm = this.getPackageManager(); 
    Intent mainIntent = new Intent(Intent.ACTION_MAIN,null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    List<ResolveInfo> list = pm.queryIntentActivities(mainIntent,0);
    Boolean isInstalled = false;
    for(ResolveInfo info: list) {
      if (info.activityInfo.applicationInfo.loadLabel( pm ).toString()==appName){
          isInstalled = true;
          break;  
      }
    } 

    return isInstalled;
}

Suppose you call checkinstalledapp ("setcpu"); The application name on the phone is called the same thing, and it should return true But it never did I recorded the result. It should match, but it didn't Anyone can inspire me why this doesn't work?

Solution

Use the equals() method of string instead of the = = operator to compare strings:

info.activityInfo.applicationInfo.loadLabel( pm ).toString().equals(appName)

One of the most common mistakes novices encounter in Java is using = = to compare strings You must remember that = = compares object references, not content

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>