例說 android:mimeType

來源:互聯網
上載者:User

執行個體代碼為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系統中,直到卸載這個應用程式。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.