實例代碼爲SDK自帶的sample NotePad
startActivity(new Intent(Intent.ACTION_EDIT, uri));
其中uri爲:content://com.google.provider.NotePad/notes/1
要啓動的activity爲<activity android:name="NoteEditor"
android:theme="@android:style/Theme.Light"
android:label="@string/title_note"
android:screenOrientation="sensor"
android:configChanges="keyboardHidden|orientation"
>
<!-- This filter says that we can view or edit the data of
a single note -->
<intent-filter android:label="@string/resolve_edit">
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<action android:name="com.android.notepad.action.EDIT_NOTE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.item/vnd.google.note" />
</intent-filter>
<!-- This filter says that we can create a new note inside
of a directory of notes. -->
<intent-filter>
<action android:name="android.intent.action.INSERT" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="vnd.android.cursor.dir/vnd.google.note" />
</intent-filter>
</activity>
隱形Intent如何找到其對定的Activity?
1.系統從intent中獲取道uri,得到了content://com.google.provider.NotePad/notes/1,
去掉開始的content:標識,得到com.google.provider.NotePad/notes/1,
然後獲取前面的com.google.provider.NotePad,然後就到Androidmanfest.xml中
找到authorities爲com.google.provider.NotePad的provider,
然後就加載這個content provider <provider android:name="NotePadProvider"
android:authorities="com.google.provider.NotePad"
/>
2.然後調用NotePadProvider的gettype函數,並把上述URI傳給這個函數,
函數返回URI所對應的類型,這裏返回Notes.CONTENT_ITEM_TYPE,代表一條日誌記錄,
而CONTENT_ITEM_TYPE = " vnd.android.cursor.item/vnd.google.note "
@Override
public String getType(Uri uri) {
switch (sUriMatcher.match(uri)) {
case NOTES:
return Notes.CONTENT_TYPE;
case NOTE_ID:
return Notes.CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI " + uri);
}
}
3.然後系統使用獲得的" vnd.android.cursor.item/vnd.google.note "和
”android.intent.action.EDIT”到androidmanfest.xml中去找匹配的activity.其中:android:authorities="com.google.provider.NotePad" 這段代碼是指定此ContentProvider的authorities,
類似於activity中的IntentFilter中action的作用,說白了就是這個ContentProvider在一個
android系統中的名字。ContentProvider在這個應用程序啓動以後,
就會永遠存在android系統中,直到卸載這個應用程序。