Android之內容提供者ContentProvider的總結

來源:互聯網
上載者:User

標籤:

本文包含以下知識點:

  ContentProvider
  Uri 的介紹
  ContentResolver:

  監聽ContentProvider的資料改變

 

一:ContentProvider部分

ContentProvider的作用:

  Content providers store and retrieve data and make it accessible to all applications. They‘re the only way to share data across applications;

there‘s no common storage area that all Android packages can access.

  內容提供者作為一個應用程式共用其資料的唯一介面供其他的應用程式訪問。比如我開發一個程式:想訪問連絡人,那麼就必須通過連絡人裡的ContentProvider來擷取資料。

  而如果別的應用程式想訪問我的資料,必須看我個人的意願,我意願建立什麼樣的ContentProvider願意共用出什麼資料,別人才能看到這些資料。可以理解為我有意通 過

  ContentProvider來暴露我的資料。

ContentProvider的資料存放區的形式:

  Content providers expose their data as a simple table on a database model, where each row is a record and each column is data of a particular type and meaning.

  以表格的形式暴露資料:

  For example, information about people and their phone numbers might be exposed as follows:

  

  Every record includes a numeric _ID field that uniquely identifies the record within the table. 每一條記錄都會有一個唯一的ID用於索引資料。

 


ContentProvider的建立:(注意建立完ContentProvider之後一定要在資訊清單檔中進行配置)

  建立步驟如下:

  

  那用中文解說就是:

    1.找到一個你喜歡的方式來儲存資料。

    2.繼承ContentProvider的類,注意:繼承這個類的時候要重寫以下六種方法來供調用:

    query() //查詢資料,結果集以Cursor返回
    insert() //插入資料
    update() //更新資料
    delete() //刪除資料。上面四個方法的第一個參數都要傳入一個Uri對象。
    getType() 
    onCreate()

    3.在資訊清單檔中進行配置://自己定義了一個ContentProvider一定要告訴別人不然人家怎麼知道呢

      To let the Android system know about the content provider you‘ve developed,

         declare it with a <provider> element in the application‘s AndroidManifest.xml file.

      Content providers that are not declared in the manifest are not visible to the Android system 


      

    配置時還有一個重要的參數:exported,表示該provider對象能否被其他程式訪問,當然是true;

    注意:以上的配置是最為簡單的配置,所有人只要知道了Provider的uri就能直接存取,實際可能需要加上permission許可權來提高許可權。

 

二:Uri部分:

  Uri是實現資料共用的橋樑是Android資源的唯一標識,相當於web中的URL一樣。

public static final Uri CONTENT_URI = 
               Uri.parse("content://com.example.codelab.transportationprovider");

 

    

 

三:ContentResolver:

ContentProvider的擷取和使用就要結合:ContentResolver和Uri來一起配合:
  forexample:在我的應用程式裡調用連絡人:

    ContentResolver cr = getContentResolver();

    //擷取系統的resolver對象,注意此時並不知道要操作哪一個provider的資料。

    //需要一個uri參數在調用參數時與之匹配

    

      CONTENT_URI =Uri.parse("content://com.example.codelab.transportationprovider");

    cr可以調用增刪改查的方法,通過Uri的參數實際上是調用對應ContentProvider的增刪改查的方法。

 四:內容觀察者:ContentObserver:

  當資料共用出來之後,可以通過相關方法使得Provider裡的資料發生改變,Android提供了內容觀察者ContentObserver來監聽其資料的改變。

  在實際使用時,我們需要繼承ContentObserver的類並重寫onChange()方法用於監聽資料的改變。

  為了監聽指定ContentProvider的資料改變,還需要通過ContentResolver向指定Uri註冊內容觀察者監聽器:

  

  參數介紹:1.uri指定ContentProvider。2.boolean值:假設:uri=content://abc,如果為true,則:content://abc/d資料改變就會觸發監聽,

        如果為flase:則不會觸發。

        3自訂的ContentObserver.

Android之內容提供者ContentProvider的總結

聯繫我們

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