Default when using multiple Actions in Intent-Filt

2019-06-24 00:36发布

问题:

Trying to grok intents and actions in android and looking through the documentation. But one thing I keep seeing is an intent filter with multiple actions defined. Like this, from the above link:

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <action android:name="android.intent.action.EDIT" />
    <action android:name="android.intent.action.PICK" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>

But, if you call that activity, how does it choose which action is chosen?

For that matter, that linked to example has multiple activities that all contain the same actions, "android.intent.action.VIEW" for example. When calling this with something like content://com.google.provider.NotePad/notes how does it even know which activity to use?

回答1:

But, if you call that activity, how does it choose which action is chosen?

The Intent has an action. If that action matches one of the three in the Intent filter, and matches on the category, and matches on the MIME type, then it will match the Intent filter overall and will start the activity.

In other words, multiple actions (or any other element) are a logical OR.

For that matter, that linked to example has multiple activities that all contain the same actions, "android.intent.action.VIEW" for example.

And generally there is stuff in the Intent filters to distinguish one from the next.

When calling this with something like content://com.google.provider.NotePad/notes how does it even know which activity to use?

It asks the content provider, "yo, dawg -- what's the MIME type for this, yo?". Given the MIME type from the content provider, it can find any matching Intent filters.