目錄
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","[email protected]")
.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