Start Activity with action, but no category

2019-04-26 17:56发布

I am trying to start an activity defined in another apk, in its AndroidManifest.xml, it defines an activity and with an action, but no category defined.

The format is like

<activity name="...">
    <intent-filter>
        <action android:name="action name">
    <intent-filter>
</activity>

My code is following

Intent i = new Intent("action name");
startActivity(i);

However my apk crashed with uncaught ActivityNotFound exception, the logs read No Activity found to handle intent ... "

Any thoughts?

Thanx a lot.

3条回答
贪生不怕死
2楼-- · 2019-04-26 18:21

You need to define the activity you are starting in your Manifest. Make sure you have provided same <intent-action (and name of the activity) that has the activity in the other apk you want to start.

android: how do i open another app from my app?

查看更多
冷血范
3楼-- · 2019-04-26 18:41

you cannot have empty category when you use startActivity(...).

add a default category and this will do the job:

<intent-filter>
    <action android:name="action name" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
查看更多
We Are One
4楼-- · 2019-04-26 18:45

Looking at the Intent documentation, it says Note also the DEFAULT category supplied here: this is required for the Context.startActivity method to resolve your activity when its component name is not explicitly specified. If the activity's IntentFilter definition does not include that category then you can't start it with startActivity. Try using the setClassName method, and pass it the package class and the activity class you're trying to launch.

查看更多
登录 后发表回答