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

后端 未结 2 1193
情深已故
情深已故 2020-12-09 16:28

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

2条回答
  •  离开以前
    2020-12-09 16:53

    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.

提交回复
热议问题