步步爲營_Android開發課[6]_ContentProvider學習

Focus on technology, enjoy life!—— QQ:804212028
瀏覽鏈接:http://blog.csdn.net/y18334702058/article/details/44624305


  • 主題:ContentProvider學習

ContentProvider是什麼

ContentProvider(內容提供者)是Android中的四大組件之一。主要用於對外共享數據,也就是通過ContentProvider把應用中的數據共享給其他應用訪問,其他應用可以通過ContentProvider對指定應用中的數據進行操作。所以如果你想實現不同應用之間的數據共享,就不得不用content provider了。

ContentProvider的使用

1.主要方法有:

public boolean onCreate() 在創建ContentProvider時調用
public Cursor query(Uri, String[], String, String[], String) 用於查詢指定Uri的ContentProvider,返回一個Cursor
public Uri insert(Uri, ContentValues) 用於添加數據到指定Uri的ContentProvider中
public int update(Uri, ContentValues, String, String[]) 用於更新指定Uri的ContentProvider中的數據
public int delete(Uri, String, String[]) 用於從指定Uri的ContentProvider中刪除數據
public String getType(Uri) 用於返回指定的Uri中的數據的MIME類型

*如果操作的數據屬於集合類型,那麼MIME類型字符串應該以vnd.android.cursor.dir/開頭。
例如:要得到所有person記錄的Uri爲content://contacts/person,那麼返回的MIME類型字符串爲”vnd.android.cursor.dir/person”。

*如果要操作的數據屬於非集合類型數據,那麼MIME類型字符串應該以vnd.android.cursor.item/開頭。
例如:要得到id爲10的person記錄的Uri爲content://contacts/person/10,那麼返回的MIME類型字符串應爲”vnd.android.cursor.item/person”。

2.使用實例(獲取通訊錄中電話號碼):

先在AndroidMainfest.xml裏註冊讀取通訊錄權限:

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

MainActivity源代碼:

package com.example.demo2;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Contacts;
import android.provider.ContactsContract.PhoneLookup;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    private ListView listView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView = (ListView)this.findViewById(R.id.listview);
        List<String> list = new ArrayList<String>();

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

        while(cursor.moveToNext()){

            String contactString = cursor.getString(cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME));

            String contactID = cursor.getString(cursor.getColumnIndex(Contacts._ID));
            Cursor phoneCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI
                    ,null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+" = "+contactID, null,null);

            StringBuffer allphonenum = new StringBuffer();
            while (phoneCursor.moveToNext()) {
                    String phoneString =phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                allphonenum.append(phoneString+"\n");
            }
            list.add(contactString+":\n"+allphonenum.toString());           
        } 

        ListAdapter listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,list);
        listView.setAdapter(listAdapter);   
    }
}

activity_main.xml源代碼:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <ListView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:id="@+id/listview"
    android:scrollbars="vertical"
    />
</LinearLayout>

運行結果爲:
這裏寫圖片描述
我的模擬器裏的通訊錄裏因爲只存一個聯繫人,所以只顯示一個聯繫人。
Focus on technology, enjoy life!—— QQ:804212028
瀏覽鏈接:http://blog.csdn.net/y18334702058/article/details/44624305

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章