It’s time to give up this method. I don’t know how many people this method has harmed. Let’s take a look at the Intent source code
As other respondents said, there is no verification of the component, so no matter what setClassName parameter you set, the returned ComponentName must not be empty, so there is always a problem with the activity mentioned by the questioner. Solution, use another method
Intent intent = new Intent();
intent.setClassName(getPackageName(), "xxx.xxx.XxxActivity");
ResolveInfo resolveInfo = getPackageManager().resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
if(resolveInfo != null) {
//activity found
}else{
//activity not found
}
Note that the second parameter of setClassName is 包名+类名
resolveActivity simply returns the value set by setClassName There is no verification of whether the class exists. If it is in the same process, it is recommended to use Class.forName(className) to detect whether the class exists
public Intent setClassName(String packageName, String className) {
mComponent = new ComponentName(packageName, className);
return this;
}
public ComponentName resolveActivity(PackageManager pm) {
if (mComponent != null) {
return mComponent;
}
...
}
It’s time to give up this method. I don’t know how many people this method has harmed. Let’s take a look at the Intent source code
As other respondents said, there is no verification of the component, so no matter what setClassName parameter you set, the returned ComponentName must not be empty, so there is always a problem with the activity mentioned by the questioner.
Solution, use another method
Note that the second parameter of setClassName is
包名+类名
This method is the simplest and crudest
You can use ActivityManager to judge
resolveActivity simply returns the value set by setClassName
There is no verification of whether the class exists.
If it is in the same process, it is recommended to use Class.forName(className) to detect whether the class exists