嘚吧嘚
內容提供器是啥?
內容提供器(Content Provider)主要用於在不同的應用程序之間實現數據共享的功能,它提供了一套完整的機制,允許一個程序訪問另一個程序中的數據,不同於文件存儲和SharedPreferences存儲中的兩種全局可讀寫操作模式,內容提供器可以選擇只對哪一部分數據進行共享,從而保證我們程序中的隱私數據不會有泄漏的風險。
我們可以用內容提供器來幹什麼呢?
例如系統的電話簿程序,它的數據庫中保存了很多的聯繫人信息,如果這些數據都不允許第三方的程序進行訪問的話,恐怕很多應用的功能都要大打折扣了。除了電話簿之外,還有短信、媒體庫等程 序都實現了跨程序數據共享的功能,而使用的技術就是內容提供器了。
舉個大家最熟悉的例子,微信通過聯繫人添加好友。
內容提供器的使用
權限
在使用內容提供器的時候很多情況下會使用到很多危險權限
,如通訊錄、攝像機、撥打電話、發送短信… …等權限。
而一般權限
在AndroidManifest.xml
中配置一下就可以了,如
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test">
<!-- 振動權限 -->
<uses-permission android:name="android.permission.VIBRATE" />
......
</manifest>
而危險權限
不僅需要在AndroidManifest.xml
中配置,還需要用戶授權
才能使用。
權限詳情請見:Android權限詳解
獲取ContentResolver
對於每一個應用程序來說,如果想要訪問內容提供器中共享的數據,就一定要藉助ContentResolver類,可以通過Context中的getContentResolver()方法獲取到該類的實例。
ContentResolver contentResolver = getContentResolver();
ContentResolver中提供了一系列的方法用於對數據進行CRUD操作。
構建“內容URI”
和SQLiteDatabase不同的是,ContentResolver中的增、刪、改、查方法都是不接收表名參數的,而是使用一個Uri參數代替,這個參數被稱爲“內容URI”。
“內容URI”給內容提供器中的數據建立了唯一標識符,它主要由兩部分組成,權限(authority)和路徑(path)。權限是用於對不同的應用程序做區分的,一般爲了避免衝突,都會採用程序包名的方式來進行命名。
如:某個程序的包名是com.example.test
那麼該程序對應的權限就可以命名爲com.example.test.provider。
路徑則是用於對同一應用程序中不同的表做區分的,通常都會添加到權限的後面。 比如某個程序的數據庫裏存在兩張表teacher和student,這時就可以將路徑分別命名爲/teacher和/student,然後把權限和路徑進行組合,內容URI就變成了com.example.test.provider/teacher和 com.example.test.provider/student。
最後,我們還需要在字符串的頭部加上協議聲明。內容URI標準格式爲:content://com.example.test.provider/teacher和content://com.example.test.provider/student
最後調用Uri.parse()方法,將內容URI字符串解析成Uri對象。
Uri uri = Uri.parse("content://com.example.test.provider/teacher");
接下來要做的就是調用方法進行增、刪、改、查了。
增
insert()方法用於添加數據,看一下官方的介紹:
示例(僞代碼)
Uri uri = Uri.parse("content://com.example.test.provider/teacher");
ContentValues contentValues = new ContentValues();
contentValues.put("column1", "value1");
contentValues.put("column2", "value2");
contentValues.put("column3", "value3");
/**
* SQL:
* insert into table (column1,column2,column3,...)
* values (value1,value2,value3,...);
*
* 參數:
* uri 指定將數據插入某個應用程序下的某一張表(相當於SQL中的“insert into table”的“table”)
* values 添加到數據庫的一系列鍵值對(相當於SQL中的“(col1,col2,...) values (val1,val2,...)”)
*/
Uri uri1 = insert(Uri url, ContentValues values);
刪
delete()方法用於刪除數據
示例(僞代碼)
/**
* SQL:
* delete from table
* where column=value;
*
* 參數:
* uri 指定刪除某個應用程序下的某一張表中的數據(相當於SQL中的“from table”)
* where 指定約束條件(相當於SQL中的“where column=value”中的“column”)
* selectionArgs 指定的約束條件的值(相當於SQL中“where column=value”中的“value”)
*/
int resultCode = delete(Uri url, String where, String[] selectionArgs)
改
update()方法用於更新數據
(僞代碼)
/**
* SQL:
* update table
* set column1=value1,column2=value2,...
* where column=value;
*
* 參數:
* uri 指定更新某個應用程序下的某一張表(相當於SQL中的“table”)
* values 添加到數據庫的一系列鍵值對(相當於SQL中的“set column1=value1,column2=value2,...”)
* where 指定約束條件(相當於SQL中的“where column=value”中的“column”)
* selectionArgs 指定的約束條件的值(相當於SQL中“where column=value”中的“value”)
*/
int resultCode = update(Uri uri, ContentValues values, String where, String[] selectionArgs);
查
query()方法用於查詢數據
參數介紹
示例(僞代碼)
/**
* SQL:
* select column1,column2
* from table
* where column=value
* order by column;
*
* 參數:
* uri 指定查詢某個應用程序下的某一張表(相當於SQL中的“from table”)
* projection 指定查詢的列名(相當於SQL中的“select column1, column2”)
* selection 指定約束條件(相當於SQL中“where column = value”中的“column”)
* selectionArgs 指定的約束條件的值(相當於SQL中“where column=value”中的“value”)
* sortOrder 指定查詢結果的排序方式(相當於SQL中的“order by”)
*/
Cursor cursor = contentResolver.query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder)
if (cursor != null) {
while (cursor.moveToNext()) {
//獲取每一個字段的值
String column1 = cursor.getString(cursor.getColumnIndex("column1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2"));
......
}
cursor.close();
}
OK,內容提供器的基本使用介紹完了,參數解釋、(僞)代碼都很詳細,在官方文檔和各種搜索引擎(百度、Google)的幫助下圓滿完成😜。