问题描述
所以我试图比较PackageInfo和ApplicationInfo中的字符串,并在Android Studio中检索它们的包名。
So I was trying to compare the string in PackageInfo and ApplicationInfo and retrieve their package name in 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
所以当我在调试中看到问题时出现问题模式,它们实际上是相同的字符串
So the problem came by when I saw inside the debug mode, they actually HAD the same string
调试模式下的示例:
com.android.quicksearchbox
com.android.quicksearchbox
It返回 NOPE DOESNT MATCH
我真的好奇为什么会发生这种情况而且我尝试过其他形式的装饰来取消白色空间之间和之间,它仍然不会让我IT MATCHES,任何人都可以帮忙吗?
I'm really curious why this will happen and I have tried other form like trim to take off away white spaces between and no, it still does not return me "IT MATCHES", can anyone help please?
谢谢。
推荐答案
替换: if(packageName!= sysName)
使用: if(!packageName.equals(sysName.toString()))
此外,你有一个;
在结尾处
哪个不应该在那里。 (这里: if(packageName.equals(sysName.toString()));
)
Also, you have a ;
at the end of if
which shouldn't be there. (Here: if (packageName.equals(sysName.toString())) ;
)
实际上,要形成你的情况更好,你应该有一个 if-else
块而不是2 如果
s。
Actually, to form your condition better, you should have an if-else
block instead of 2 if
s.
类似于:
if (packageName.equals(sysName.toString())){
Log.d("", "IT MATCHES");
} else {
Log.d("", "NOPE DOESNT MATCH");
}
这篇关于==和.equals()在java中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!