內容提供者 ContentProvider

2021-07-10 03:40:48 字數 3736 閱讀 8662

contentprovider概念

– 內容提供者是android中的四大元件之一;

– 為儲存和獲取資料提供統一的介面;

– 可以在不同的應用程式之間共享資料;

下面幾段**實現的是建立資料庫db及表account,通過contentprovide向表中新增資料的小例子;

sqliteopenhelper:

public

class

dbhelper

extends

sqliteopenhelper

@override

public

void

oncreate(sqlitedatabase db)

@override

public

void

onupgrade(sqlitedatabase db, int oldversion, int newversion)

}

sqlitedatabase:

public

class dbutil

private

void

open() //開啟;

private

void

close() //關閉;

public

long

insert(string table, contentvalues values)

public

void

query(string table)

close();

}}

uri介紹

– 統一資源定位符,universal resource identifier的簡稱;

–每乙個contentprovider都擁有乙個公共的uri,這個uri用於表示所提供的contentprovider的資料;

– uri由以下三部分組成:「content://」、資料的路徑、標識id(可選);

– 每乙個contentprovider都擁有乙個公共的uri,這個uri用於表示所提供的contentprovider的資料;

urimatcher類的介紹

– 用於操作uri的工具類

urimatcher類的主要方法

public void adduri (string authority, string path, int code)

註冊所需的uri

public int match (uri uri) 與uri進行匹配

urimatcher類的使用

– 初始化urimatcher類

– 註冊所需的uri

– 與已經註冊的uri進行匹配

contentprovider主要方法

boolean oncreate() contentprovider建立後就執行

uri insert(uri uri, contentvalues values) 向contentprovider新增資料

cursor query(uri uri, string projection,string selection, string selectionargs, string sortorder) 從contentprovider查詢資料

int update(uri uri, contentvalues values,string selection, string selectionargs)從contentprovider更新資料

int delete(uri uri, string selection, string selectionargs)

從contentprovider刪除資料

contentprovider實現過程

– 定義content_uri常量

– 定義類繼承contentprovider

– 實現oncreate、insert、query、delete、update、gettype方法

– 在清單檔案androidmanifest.xml中宣告

自定義contentprovider步驟

– 建立儲存資料的檔案或者資料庫

– 定義類繼承contentprovider,實現抽象類

– 在清單檔案androidmanifest.xml中註冊contentprovider

public

class

mycontentvalues

extends

contentprovider

@override

public

boolean

oncreate()

@override

public cursor query(uri uri, string projection, string selection,

string selectionargs, string sortorder)

@override

public string gettype(uri uri)

@override

public uri insert(uri uri, contentvalues values)

return

null;

}@override

public

intdelete(uri uri, string selection, string selectionargs)

@override

public

intupdate(uri uri, contentvalues values, string selection,

string selectionargs)

}

新增系統contentprovider內容步驟

– 通過getcontentresolver獲取contentresolver物件

– 獲取contentresolver的uri標識

– 將新增的內容封裝到contentvalues中

– 通contentresolver物件呼叫insert方法執行新增操作

查詢系統contentprovider內容步驟

– 通過getcontentresolver獲取contentresolver物件

– 獲取contentresolver的uri標識

– 列出想要查詢的列

– contentresolver物件呼叫query方法執行查詢

public

class

mainactivity

extends

activity

implements

onclicklistener

private

void

init()

@override

public

void

onclick(view v)

}}

在清單檔案中註冊:

android:name="com.lu.contentprovider.mycontentvalues"

android:authorities="com.lu.contentprovider"

android:exported="true" //true時,其它程式通過此擴充套件訪問》

provider>

內容提供者

public class personcontentprovider extends contentprovider override public boolean oncreate 作用 判斷 傳進來的 uri 查詢的是一條資料 還是多條資料 override public string gett...

內容提供者

package com.xh.tx.utils import android.content.context import android.database.sqlite.sqlitedatabase import android.database.sqlite.sqlitedatabase.cur...

內容提供者

讀取系統簡訊,首先查詢原始碼獲得簡訊資料庫內容提供者的主機名和路徑,然後 contentresolver cr getcontentresolver cursor c cr.query uri.parse content sms new string,null,null,null while c.m...