欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Kotlin?ContentProvider使用方法介紹

 更新時間:2022年09月06日 16:42:24   作者:枕上書531  
ContentProvider內(nèi)容提供者,主要用于再不同的應用程序之前實現(xiàn)數(shù)據(jù)共享的功能,它提供了一套完整的機制,允許一個程序訪問另外一個程序的數(shù)據(jù),同時還能保證數(shù)據(jù)的安全性

1、注冊ContentProvider

右擊com.example.myapplication包->New->Other->Content Provider。會彈出窗口

點擊finish,完成創(chuàng)建ContentProvider類,這時你可以在注冊代碼中看到

<provider
            android:name=".MyContentProvider"
            android:authorities="com.example.myapplication.provider"
            android:enabled="true"
            android:exported="true"></provider>

注冊ContentProvider時通常指定屬性

屬性描述
name指定該ContentProvider的實現(xiàn)類的類名
authorities指定該ContentProvider對應的URI
enabled指定該ContentProvider是否可用
exported指定該ContentProvider是否允許其他應用調(diào)用

2、內(nèi)容URI

ContentResolver中的增刪改查方法都不接收表名參數(shù),而是使用一個Uri參數(shù)代替,這個參數(shù)被稱為內(nèi)容URI。

內(nèi)容URI的標準格式

content://<authorities>/<path>

(1)以路徑結尾:表示期望訪問該表地所有數(shù)據(jù)

(2)以id結尾:表示期望訪問該表擁有相應id的數(shù)據(jù)

<authorities>:authorities是用于對不同的應用程序做區(qū)分的,一般會采用包名的方式命名,比如包名為com.example.myapplication,那么<authorities>為com.example.myapplication.provider。

<path>:path是用于對同一應用程序的不同表做區(qū)分的,比如com.example.myapplication.provider/table1。

通配符

*表示匹配任意長度的任意字符

#表示匹配任意長度的數(shù)字

一個能夠匹配任意表的內(nèi)容URI格式

content://com.example.myapplication.provider/*

一個能夠匹配table表中任意一行數(shù)據(jù)的內(nèi)容URI格式

content://com.example.myapplication.provider/table1/#

把內(nèi)容URI字符串解析成Uri對象

val uri=Uri.parse("content://com.example.myapplication.provider/table1")

3、創(chuàng)建自己的ContentProvider

重寫ContentProvider類的6個抽象方法

  • onCreate()。初始化ContentProvider的時候調(diào)用。通常會在這里完成對數(shù)據(jù)庫的創(chuàng)建和升級等操作,返回true表示ContentProvider初始化成功,返回false則表示失敗。
  • query()。從ContentProvider中查詢數(shù)據(jù)。uri參數(shù)用于確認查詢哪張表,projection參數(shù)用于確定查詢哪些列,selection和selectionArgs參數(shù)用于約束查詢哪些行,sortOrder參數(shù)用于對結果進行排序,查詢的結果存放在Cursor對象中返回。
  • insert()。向ContentProvider中添加一條數(shù)據(jù),uri參數(shù)用于確定要添加的表,待添加的數(shù)據(jù)保存在values參數(shù)中。添加完成后,返回一個用于表示這條新紀錄的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返回相應的MIME類型。

getType方法中,一個內(nèi)容URI所對應的MIME字符串主要由3部分組成,Android對這3部分做了如下格式規(guī)定

  • 必須以vnd開頭
  • 如果內(nèi)容URI以路徑結尾,則后接android.cursor.dir/。
  • 如果內(nèi)容URI以id結尾,則后接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這個類實現(xiàn)匹配內(nèi)容URI的功能,來判斷出調(diào)用方期望訪問的時哪張表中的數(shù)據(jù)。

UriMatcher的addURI()方法,接收三個參數(shù),可以分別把authority、path和一個自定義代碼傳進去。這樣,當調(diào)用UriMatcher的match()方法時,把一個Uri對象傳入,就會返回一個與這個Uri對象匹配的一個自定義代碼。

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ù)據(jù)進行增減改查的操作。

(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"))

到此這篇關于kotlin ContentProvider使用方法介紹的文章就介紹到這了,更多相關kotlin ContentProvider內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:

相關文章

  • Android?狀態(tài)管理之Lifecycle淺析

    Android?狀態(tài)管理之Lifecycle淺析

    這篇文章主要介紹了Android?狀態(tài)管理之Lifecycle淺析,Lifecycle主要用于Activity、Fragment這一類具有狀態(tài)的組件的狀態(tài)監(jiān)聽,更多相關資料介紹需要的小伙伴可以參考下面文章內(nèi)容
    2022-06-06
  • 學習Android Handler消息傳遞機制

    學習Android Handler消息傳遞機制

    這篇文章主要為大家詳細介紹了Android Handler消息傳遞機制,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android列表實現(xiàn)(3)_自定義列表適配器思路及實現(xiàn)代碼

    Android列表實現(xiàn)(3)_自定義列表適配器思路及實現(xiàn)代碼

    Android 自定義列表適配器會提供很多的便利;下面的例子為使用自定義的列表適配器來顯示列表,感興趣的朋友可以研究下
    2012-12-12
  • Android動畫之TranslateAnimation用法案例詳解

    Android動畫之TranslateAnimation用法案例詳解

    這篇文章主要介紹了Android動畫之TranslateAnimation用法案例詳解,本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android LaunchMode四種啟動模式詳細介紹

    Android LaunchMode四種啟動模式詳細介紹

    這篇文章主要介紹了Android LaunchMode四種啟動模式詳細介紹的相關資料,這里對launchmode的使用方法進行了詳解及啟動模式的比較,需要的朋友可以參考下
    2016-12-12
  • Fragment通過FragmentManager實現(xiàn)通信功能詳細講解

    Fragment通過FragmentManager實現(xiàn)通信功能詳細講解

    這篇文章主要介紹了Fragment通過FragmentManager實現(xiàn)通信功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • Android仿抖音主頁效果實現(xiàn)代碼

    Android仿抖音主頁效果實現(xiàn)代碼

    這篇文章主要介紹了Android仿抖音主頁效果實現(xiàn),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • Android快速實現(xiàn)發(fā)送郵件實例

    Android快速實現(xiàn)發(fā)送郵件實例

    本篇文章主要介紹了Android快速實現(xiàn)發(fā)送郵件實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • Android中獲取手機屏幕大小的方法

    Android中獲取手機屏幕大小的方法

    這篇文章主要介紹了Android中獲取手機屏幕大小的方法,Android開發(fā)需要獲得屏幕的寬高,本文為大家解析 Android中如何獲取手機屏幕大小,需要的朋友可以參考下
    2015-12-12
  • Android開發(fā)Launcher進程啟動流程

    Android開發(fā)Launcher進程啟動流程

    這篇文章主要為大家介紹了Android開發(fā)Launcher進程啟動流程示例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-06-06

最新評論