Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 views
in Technique[技术] by (71.8m points)

android - intent.resolveActivity != null but launching the intent throws an ActivityNotFound exception

I post this question here for educational purposes, since I couldn't find answers anywhere and eventually found the root cause the old way, i.e. by myself.

Here's the problematic code :

// initially getting the intent from polling the PackageManager about activities resolving Search intent.

ComponentName componentName = intent.resolveActivity(pm);

if (componentName != null) {
    context.startActivity(intent);
}

despite the check i get an ActivityNotFound exception.

EDIT: apparently the point wasn't obvious to everyone so : how come there's an activity resolving the intent, yet trying to launch it throws an ActivityNotFound exception - two facts apparently contradictory ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

From what i could see, intent.resolveActivity() will return true if there is ANY activity resolving this intent. Even if this activity is not exported (which makes it unusable for all practical purposes in case it's not from your package). Android's API doesn't care to mention that, so you have to figure it out by yourself, and make sure that the activity you're trying to launch is indeed exported.

ActivityInfo activityInfo = intent.resolveActivityInfo(pm, intent.getFlags());
if (activityInfo.exported) {
    doSomething();
}

EDIT: the point of this question is that ResolveActivity will return a componentName even if activityInfo.exported==false AND it's not from your own package - which makes it unlaunchable, and surprised me cause the intent was resolved and yet unlaunchable.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...