10天學通Android開發(3-2)-組件ContentProvider

來源:互聯網
上載者:User

標籤:android   update   insert   contentprovider   

1)ContentProvider簡介
      
該類用於提供資料和儲存資料的方法,就可以向其他應用共用其資料。雖然使用其他方法也可以對外共用資料,但資料訪問方式會因資料存放區的方式而不同,如:採用檔案方式對外共用資料,需要進行檔案操作讀寫資料;採用sharedpreferences共用資料,需要使用sharedpreferences API讀寫資料。而使用ContentProvider共用資料的好處是統一了資料訪問方式。

2)每個ContentProvider都有一個公用的URI,這個URI用於表示這個ContentProvider所提供的資料。Android所提供的ContentProvider都存放在android.provider包當中,Uri類,如:

      Uri uri = Uri.parse("content://com.changcheng.provider.contactprovider/contact")

     

 

3)ContentProvider提供的方法
query
:查詢
insert
:插入
update
:更新
delete
:刪除
getType
:得到資料類型
onCreate
:建立資料時調用的回呼函數

在Content Provider中使用的查詢字串有別於標準的SQL查詢

 

 

案例:讀取連絡人資料為例

  1. 建立一項目,先在AndroidManifest.xml中設定可讀取連絡人的許可權:

Permission 中’Add’,UserPermission

android.permission.READ_CONTACTS

 

實際上AndroidManifest.xml添加了一行:

  <uses-permissionandroid:name="android.permission.READ_CONTACTS"/>

2) 讀取連絡人姓名

protectedvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

     Cursor c=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

       while(c.moveToNext()){        System.out.println(">>>>>>>>>"+c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));

          

       }

 

 

參考:

http://www.cnblogs.com/devinzhang/archive/2012/01/20/2327863.html

http://xiechengfa.iteye.com/blog/1415829


本文出自 “藍海戰術” 部落格,請務必保留此出處http://wanxl.blog.51cto.com/2129901/1592776

10天學通Android開發(3-2)-組件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.