例说 android:mimeType

来源:互联网 发布:企业电话搜索软件 编辑:程序博客网 时间:2024/05/21 22:41

实例代码为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系统中,直到卸载这个应用程序。