Kotlin?ContentProvider使用方法介紹
1、注冊(cè)ContentProvider
右擊com.example.myapplication包->New->Other->Content Provider。會(huì)彈出窗口

點(diǎn)擊finish,完成創(chuàng)建ContentProvider類,這時(shí)你可以在注冊(cè)代碼中看到
<provider
android:name=".MyContentProvider"
android:authorities="com.example.myapplication.provider"
android:enabled="true"
android:exported="true"></provider>注冊(cè)ContentProvider時(shí)通常指定屬性
| 屬性 | 描述 |
| name | 指定該ContentProvider的實(shí)現(xiàn)類的類名 |
| authorities | 指定該ContentProvider對(duì)應(yīng)的URI |
| enabled | 指定該ContentProvider是否可用 |
| exported | 指定該ContentProvider是否允許其他應(yīng)用調(diào)用 |
2、內(nèi)容URI
ContentResolver中的增刪改查方法都不接收表名參數(shù),而是使用一個(gè)Uri參數(shù)代替,這個(gè)參數(shù)被稱為內(nèi)容URI。
內(nèi)容URI的標(biāo)準(zhǔn)格式
content://<authorities>/<path>
(1)以路徑結(jié)尾:表示期望訪問該表地所有數(shù)據(jù)
(2)以id結(jié)尾:表示期望訪問該表?yè)碛邢鄳?yīng)id的數(shù)據(jù)
<authorities>:authorities是用于對(duì)不同的應(yīng)用程序做區(qū)分的,一般會(huì)采用包名的方式命名,比如包名為com.example.myapplication,那么<authorities>為com.example.myapplication.provider。
<path>:path是用于對(duì)同一應(yīng)用程序的不同表做區(qū)分的,比如com.example.myapplication.provider/table1。
通配符
*表示匹配任意長(zhǎng)度的任意字符
#表示匹配任意長(zhǎng)度的數(shù)字
一個(gè)能夠匹配任意表的內(nèi)容URI格式
content://com.example.myapplication.provider/*
一個(gè)能夠匹配table表中任意一行數(shù)據(jù)的內(nèi)容URI格式
content://com.example.myapplication.provider/table1/#
把內(nèi)容URI字符串解析成Uri對(duì)象
val uri=Uri.parse("content://com.example.myapplication.provider/table1")3、創(chuàng)建自己的ContentProvider
重寫ContentProvider類的6個(gè)抽象方法
- onCreate()。初始化ContentProvider的時(shí)候調(diào)用。通常會(huì)在這里完成對(duì)數(shù)據(jù)庫(kù)的創(chuàng)建和升級(jí)等操作,返回true表示ContentProvider初始化成功,返回false則表示失敗。
- query()。從ContentProvider中查詢數(shù)據(jù)。uri參數(shù)用于確認(rèn)查詢哪張表,projection參數(shù)用于確定查詢哪些列,selection和selectionArgs參數(shù)用于約束查詢哪些行,sortOrder參數(shù)用于對(duì)結(jié)果進(jìn)行排序,查詢的結(jié)果存放在Cursor對(duì)象中返回。
- insert()。向ContentProvider中添加一條數(shù)據(jù),uri參數(shù)用于確定要添加的表,待添加的數(shù)據(jù)保存在values參數(shù)中。添加完成后,返回一個(gè)用于表示這條新紀(jì)錄的URI。
- updata()。更新ContentProvider中已有的數(shù)據(jù),uri參數(shù)用于確定更新哪一張表中的數(shù)據(jù),新數(shù)據(jù)保存在values參數(shù)中,selection和selectionArgs參數(shù)用于約束更新哪些行,受影響的行數(shù)將作為返回值返回。
- delete()。從ContentProvider中刪除數(shù)據(jù)。uri參數(shù)用于確定刪除哪一張表中的數(shù)據(jù),selection和selectionArgs參數(shù)用于約束刪除哪些行,被刪除的行數(shù)將作為返回值返回。
- getType()。根據(jù)傳入的內(nèi)容URI返回相應(yīng)的MIME類型。
getType方法中,一個(gè)內(nèi)容URI所對(duì)應(yīng)的MIME字符串主要由3部分組成,Android對(duì)這3部分做了如下格式規(guī)定
- 必須以vnd開頭
- 如果內(nèi)容URI以路徑結(jié)尾,則后接android.cursor.dir/。
- 如果內(nèi)容URI以id結(jié)尾,則后接android.cursor.item/。
- 最后接上vnd.<acthority>.<path>。
例子:content://com.example.myapplication.provider/table1
MIME類型:vnd.android.cursor.dir/vnd.com.example.myapplication.provider.table1
override fun delete(uri: Uri, selection: String?, selectionArgs: Array<String>?): Int {
}
override fun getType(uri: Uri): String? {
}
override fun insert(uri: Uri, values: ContentValues?): Uri? {
}
override fun onCreate(): Boolean {
}
override fun query(uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String?): Cursor? {
}
override fun update(uri: Uri, values: ContentValues?, selection: String?,
selectionArgs: Array<String>?): Int {
}(2)利用UriMatcher這個(gè)類實(shí)現(xiàn)匹配內(nèi)容URI的功能,來判斷出調(diào)用方期望訪問的時(shí)哪張表中的數(shù)據(jù)。
UriMatcher的addURI()方法,接收三個(gè)參數(shù),可以分別把a(bǔ)uthority、path和一個(gè)自定義代碼傳進(jìn)去。這樣,當(dāng)調(diào)用UriMatcher的match()方法時(shí),把一個(gè)Uri對(duì)象傳入,就會(huì)返回一個(gè)與這個(gè)Uri對(duì)象匹配的一個(gè)自定義代碼。
class MyContentProvider : ContentProvider() {
private val table1Dir=0
private val table1Item=1
private val table2Dir=2
private val table2Item=3
private val uriMatcher=UriMatcher(UriMatcher.NO_MATCH)
init {
uriMatcher.addURI("com.example.app.provider","table1",table1Dir)
uriMatcher.addURI("com.example.app.provider","table1/#",table1Item)
uriMatcher.addURI("com.example.app.provider","table2",table2Dir)
uriMatcher.addURI("com.example.app.provider","table2Item",table2Item)
}
...
override fun query(uri: Uri, projection: Array<String>?, selection: String?,
selectionArgs: Array<String>?, sortOrder: String?): Cursor?
{
when(uriMatcher.match(uri)){
table1Item->{
//查詢table1表中的所有數(shù)據(jù)
}
table1Item->{
//查詢table1表中的單條數(shù)據(jù)
}
table2Dir->{
//查詢table2表中的所有數(shù)據(jù)
}
table2Item->{
//查詢table2表中的單條數(shù)據(jù)
}
}
}
...
}4、訪問其他程序中的數(shù)據(jù)
1、ContentResolver的基本用法
要訪問ContentProvider中共享的數(shù)據(jù),就要調(diào)用Context的getContentResolver()方法獲取ContentResolver的實(shí)例,然后對(duì)數(shù)據(jù)進(jìn)行增減改查的操作。
(1)查詢
val cursor=contentResolver.query(uri,projection,selection,selectionArgs,sortOrder)
if (cursor != null) {
while (cursor.moveToNext()){
val column1=cursor.getString(cursor.getColumnIndex("column1"))
val column2=cursor.getString(cursor.getColumnIndex("colum2"))
}
cursor.close()
}(2)增加
val values= contentValuesOf("column1" to "text","column2" to 1)
contentResolver.insert(uri,values)(3)修改
val values= contentValuesOf("column1" to "")
contentResolver.update(uri, values,"column1 = ? and column2 = ?", arrayOf("text","1"))(4)刪除
contentResolver.delete(uri,"column2 = ?", arrayOf("1"))到此這篇關(guān)于kotlin ContentProvider使用方法介紹的文章就介紹到這了,更多相關(guān)kotlin ContentProvider內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android的VSYNC機(jī)制和UI刷新流程示例詳解
這篇文章主要為大家介紹了Android的VSYNC機(jī)制和UI刷新流程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
使用CMake構(gòu)建OpenCV項(xiàng)目過程解析
這篇文章主要介紹了使用CMake構(gòu)建OpenCV項(xiàng)目過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
android將圖片轉(zhuǎn)換存到數(shù)據(jù)庫(kù)再?gòu)臄?shù)據(jù)庫(kù)讀取轉(zhuǎn)換成圖片實(shí)現(xiàn)代碼
有時(shí)候我們想把圖片存入到數(shù)據(jù)庫(kù)中,盡管這不是一種明智的選擇,但有時(shí)候還是不得以會(huì)用到,下面說說將圖片轉(zhuǎn)換成byte[]數(shù)組存入到數(shù)據(jù)庫(kù)中去,并從數(shù)據(jù)庫(kù)中取出來轉(zhuǎn)換成圖像顯示出來2013-11-11
Flutter仿網(wǎng)易實(shí)現(xiàn)廣告卡片3D翻轉(zhuǎn)效果
在逛網(wǎng)易新聞時(shí),發(fā)現(xiàn)列表中的廣告在你滑動(dòng)的時(shí)候會(huì)有一個(gè)3D旋轉(zhuǎn)的交互引你的注意。本文將利用Flutter實(shí)現(xiàn)這一效果,感興趣的可以了解一下2022-04-04
快速解決fragment中onActivityResult不調(diào)用的問題
下面小編就為大家?guī)硪黄焖俳鉀Qfragment中onActivityResult不調(diào)用的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04
一文了解Android?ViewModelScope?如何自動(dòng)取消協(xié)程
這篇文章主要介紹了一文了解Android?ViewModelScope?如何自動(dòng)取消協(xié)程,文章圍繞主題站展開詳細(xì)的內(nèi)容介紹,具有一定參考價(jià)值,感興趣的小伙伴可以參考一下2022-07-07
Android短信驗(yàn)證碼(用的Mob短信驗(yàn)證)
這篇文章主要為大家詳細(xì)介紹了Android短信驗(yàn)證碼,使用Mob短信驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

