Android Content Provider簡介

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

  Content Provider是Android的四大組件之一,與Activity和Service相同,使用之前需要註冊;

  Android系統中存在大量的應用,當不同的應用程式之間需要共用資料時,可以使用Content Provider來實現,因為它為儲存和讀取資料提供了統一的介面;

 

(1)Android系統內建的許多資料都是使用Content Provider,然後供開發人員調用,如音頻,視頻,圖片,通訊錄等;

(2)當一個程式需要把自己的資料暴露給其他程式使用時,該程式就可以通過提供Content Provider來實現,而其他程式就可以通過ContentResolver來操作Content Provider暴露的資料;

(3)應用程式通過ContentProvider開放了自己的資料,該應用程式不需要啟動,其他應用程式都可以操作開放的資料,包括增刪改查操作;

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

 

ContentProvider類的使用步驟

1、自訂MyProvider extends ContentProvider子類,在該類中重寫用於增刪改查的操作;

2、在AndroidManifest.xml中註冊該類;

3、設定共用資料的uri地址;

4、在其它類或其他項目中調用ContentResolver對象中的增刪改查方法,按uri提供的地址對資料進行操作;

 

例如:擷取系統的手機的通訊錄;

 

 

代碼其實很簡單,因為我們沒有自訂我們的Content Provider類,所以只使用ContentResolver來操作資料就行了;

package com.xiaozhang.contentprovidertest;import android.app.Activity;import android.os.Bundle;import android.provider.ContactsContract;import android.provider.ContactsContract.PhoneLookup;import android.database.Cursor;import android.graphics.Color;import android.widget.TextView;import android.content.ContentResolver;public class MainActivity extends Activity {    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        TextView textView = new TextView(this);        String string = getQueryData();        textView.setTextColor(Color.GRAY);        textView.setText(string);        textView.setTextSize(20.0f);        setContentView(textView);    }    public String getQueryData() {        String string = "";        int id = 1;        // 得到ContentResolver對象        ContentResolver cr = getContentResolver();        // 擷取電話本中開始的遊標        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,                null, null, null);        // 向下移動        while (cursor.moveToNext()) {            // 取得連絡人名字            int nameFieldColumnIndex = cursor                    .getColumnIndex(PhoneLookup.DISPLAY_NAME);            String contact = cursor.getString(nameFieldColumnIndex);            // 取得電話號碼            String ContactId = cursor.getString(cursor                    .getColumnIndex(ContactsContract.Contacts._ID));            Cursor phone = cr.query(                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "="                            + ContactId, null, null);            while (phone.moveToNext()) {                String PhoneNumber = phone                        .getString(phone                                .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                string += (id + "-----" + contact + "-----" + PhoneNumber + "\n");                id++;            }        }        cursor.close();        return string;    }}

然後在AndroidManifest.xml中添加許可權;

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

 

Android Content Provider簡介

聯繫我們

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