目錄
1.概念講解
2.示例(訪問手機通訊錄)
1.概念講解
(1)ContentProvider的概述:
當我們想允許自己的應用資料允許別的應用進行讀取操作,我們可以讓我們的App實作ContentProvider類,同時注冊一個Uri,然后其他應用只要使用ContentResolver根據Uri就可以操作我們的App中的資料了!而資料庫不一定是資料庫,也可能是檔案,xml,或者其他,但是SharePreference使用基于資料庫模型的簡單表格來提供其中的資料!
(2)ContentProvider的執行原理

(3)URI簡介:
專業名詞叫做:通用資源識別符號,我們也可以類比為網頁的域名,就是定位資源所在路徑,例如:
Uri uri = Uri.parse(“content://com.andriod.contacts/raw_contacts”);
content:協議頭,這是規定的,就像http,ftp等一樣,而ContentProvider規定的是content開頭的,接著是provider所在的全限定類名,
com.andriod.contacts代表資源部分,如果想訪問com.andriod.contacts所有資源,后面就不用寫了,
raw_contacts訪問的是com.andriod.contacts資源中id為 raw_contacts的記錄,
2.示例(訪問手機通訊錄)
(1)打開android studio新建一個專案,創建一個ContentProviderActivity.java檔案和layout.xml檔案,如圖:

layout.xml檔案
(2)寫入兩個按鈕
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/btn_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="獲取資訊"/>
<Button
android:id="@+id/btn_add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="添加聯系人"/>

ContentProviderActivity.java檔案
(3)獲取讀、寫的動態權限
注意:需要在AndroidManifest.xml檔案中添加讀寫的權限獲取代碼
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
下面為java代碼塊
int permissionCheck = ContextCompat.checkSelfPermission(this,Manifest.permission.READ_CONTACTS);
if(permissionCheck != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},0);
}
public void onRequestPermissionsResult(int requestCode,String permissions[],int[] grantResults){
switch (requestCode){
case 0:
if ((grantResults.length > 0)&&(grantResults[0] == PackageManager.PERMISSION_GRANTED)){
Toast.makeText(ContentProviderActivity.this,"聯系人授權成功",Toast.LENGTH_SHORT).show();
int permissionCheck = ContextCompat.checkSelfPermission(this,Manifest.permission.WRITE_CONTACTS);
if(permissionCheck != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_CONTACTS},1);
}
}
break;
case 1:
if ((grantResults.length > 0)&&(grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
Toast.makeText(ContentProviderActivity.this, "寫入聯系人權限授權成功", Toast.LENGTH_SHORT).show();
}
default:
break;
}
}
(4)獲取聯系人資訊
private void getContacts(){
//查詢raw_contacts表獲得聯系人的id
ContentResolver resolver = getContentResolver();
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
//查詢聯系人資料
Cursor cursor = resolver.query(uri,null,null,null,null);
while (cursor.moveToNext()){
//獲取聯系人姓名,手機號碼
String cName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String cNum = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Log.e("","姓名:"+cName);
Log.e("","號碼:"+cNum);
Log.e("","=====================");
}
cursor.close();
}

(5)添加聯系人
private void addContact(){
try {
//使用事務添加聯系人
Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
Uri dataUri = Uri.parse("content://com.android.contacts/data");
ContentResolver resolver = getContentResolver();
ArrayList<ContentProviderOperation> operations = new ArrayList<>();
//為了便于Android中進行批量資料庫操作時效率更高,Android中推薦使用ContentProviderOperation
ContentProviderOperation op1 = ContentProviderOperation.newInsert(uri)
.withValue("account_name",null)
.build();
operations.add(op1);
//依次是姓名,號碼,郵編
ContentProviderOperation op2 =ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id",0)
.withValue("mimetype","vnd.android.cursor.item/name")
.withValue("data2","Harden")
.build();
operations.add(op2);
ContentProviderOperation op3 =ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id",0)
.withValue("mimetype","vnd.android.cursor.item/phone_v2")
.withValue("data1","15263348325")
.build();
operations.add(op3);
ContentProviderOperation op4 =ContentProviderOperation.newInsert(dataUri)
.withValueBackReference("raw_contact_id",0)
.withValue("mimetype","vnd.android.cursor.item/email_v2")
.withValue("data1","1456237895@qq.com")
.build();
operations.add(op4);
//將上述內容添加到手機聯系人中
resolver.applyBatch("com.android.contacts",operations);
Toast.makeText(getApplicationContext(),"添加成功",Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Log.e("",e.getMessage());
}
}
}


作者:陳紹波
原文鏈接:
https://blog.csdn.net/qq_44766524/article/details/112001082?utm_source=app
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/243871.html
標籤:其他
下一篇:C語言100題之第一題及變式:四個數字分別為1,2,3,4,能組成多少個互不相同且無重復數字的三位數,它們分別是什么?
