==和.equals()在java中不起作用

时间:2016-03-09 18:38:01

标签: java android string android-studio package-managers

所以我试图比较PackageInfo和ApplicationInfo中的字符串,并在Android Studio中检索它们的包名。

    List<ApplicationInfo> packages = pm.getInstalledApplications(PackageManager.GET_META_DATA);
    List<PackageInfo> apps = pm.getInstalledPackages(PackageManager.GET_PERMISSIONS);

            for (PackageInfo app : apps)
                for(ApplicationInfo packageInfo:packages)
                        String packageName = packageInfo.packageName;
                        String sysName = app.packageName;

                        if (packageName != sysName)
                        {
                            Log.d("", "NOPE DOESNT MATCH");
                            break;
                        }
                        if (packageName.equals(sysName.toString())) ;
                        {
                            Log.d("", "IT MATCHES");
                        //do something after that

所以当我在调试模式中看到问题时,他们实际上遇到了相同的字符串

调试模式下的示例:

com.android.quicksearchbox

com.android.quicksearchbox

返回“ NOPE DOESNT MATCH

我真的很好奇为什么会发生这种情况而且我尝试了其他形式的装饰来消除白色空间和不消息,它仍然没有让我“IT MATCHES”,有人可以帮忙吗?

感谢。

1 个答案:

答案 0 :(得分:1)

替换:if (packageName != sysName)

使用:if (!packageName.equals(sysName.toString()))

此外,;末尾的if不应该出现在if (packageName.equals(sysName.toString())) ;。 (这里:if-else

实际上,为了更好地形成您的条件,您应该使用if块而不是2 if (packageName.equals(sysName.toString())){ Log.d("", "IT MATCHES"); } else { Log.d("", "NOPE DOESNT MATCH"); }

类似的东西:

SELECT a.IP, a.hits, b.clicks, c.bought
FROM (select IP, count (IP) as hits from A group by IP) a
JOIN (select IP, count(IP) as clicks from table A join table b on A.SID = B.SID group by IP) b on a.ip = b.ip
JOIN (select IP, count(IP) as bought from A join B on A.SID = B.SID join C on C.SID = B.SID group by IP) c ON a.ip = c.ip