android ContentProvider中getType(Uri uri)的理解

來源:互聯網
上載者:User

很多朋友在用ContentProvider的時候,對複寫的方法getType(Uri uri)不理解,也不知道這個方法是在什麼時候被調用的,今天就為大家解釋一下。

  
當我們在程式中需要啟動例外的activity時,很多時候我們是使用的隱式調用,即我們不直接指定要跳轉的Activity,而是為Intent提供一
些相關的參數,讓其自動的去和AndroidManifest.xml中已有的Activity去匹配,而IntentFilter(Intent
過濾器)在xml中有三個主要的參數:action,categary,data。

我們通過Intent的構造函數或者Intent提供的方法可以指定這三個參數,如方法有:

            intent.setAction(action);
            intent.setData(data);
            intent.addCategory(category);

等,比如在記事本程式中有:

         <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>

我們很容易看出action和category是很容易匹配的,而我們傳的Uri的數據怎麼匹配呢,這時系統就會去調用你定義的
ContentProvider中的getType,取得相關的傳回值來和上面的data串進行匹配,當然getType的返回結果你是需要自己去定義
的。

但在程式中你也可以自己知道data的類型,就直接匹配了:intent.setType(type);

如果大家還不清楚,那麼請看這個鏈接:
http://littlefermat.blog.163.com/blog/static/59771167201062510044904/

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.