使用ContentProvider共享資料

2021-08-27 16:34:41 字數 1725 閱讀 1022

使用contentprovider共享資料:

public boolean oncreate()

該方法在contentprovider建立後就會被呼叫,android開機後, contentprovider在其它應用第一次訪問它時才會被建立。

public uri insert(uri uri, contentvaluesvalues)

該方法用於供外部應用往contentprovider新增資料。

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

該方法用於供外部應用從contentprovider刪除資料。

public int update(uri uri,contentvaluesvalues, string selection, string selectionargs)

該方法用於供外部應用更新contentprovider中的資料。

public cursor query(uri uri,string projection, string selection, string selectionargs,string sortorder)

該方法用於供外部應用從contentprovider中獲取資料。

public string gettype(uriuri)

該方法用於返回當前url所代表資料的mime型別。如果操作的資料屬於集合型別,那麼mime型別字串應該以vnd.android.cursor.dir/開頭,

例如:要得到所有person記錄的uri為content:/com.qiuzhping.providers.userprovider/user,

那麼返回的mime型別字串應該為:「vnd.android.cursor.dir/user」。如果要操作的資料屬於非集合型別資料,

那麼mime型別字串應該以vnd.android.cursor.item/開頭,例如:得到id為10的user記錄,

uri為content:那麼返回的mime型別字串應該為:「vnd.android.cursor.item/user」。

當外部應用需要對contentprovider中的資料進行新增、刪除、修改和查詢操作時,可以使用contentresolver 類來完成,

要獲取contentresolver 物件,可以使用activity提供的getcontentresolver()方法。

contentresolver 類提供了與contentprovider類相同簽名的四個方法:

public uri insert(uri uri, contentvalues values)

該方法用於往contentprovider新增資料。

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

該方法用於從contentprovider刪除資料。

public int update(uri uri, contentvalues values, string selection, string selectionargs)

該方法用於更新contentprovider中的資料。

public cursor query(uri uri, string projection, string selection, string selectionargs, string sortorder)

該方法用於從contentprovider中獲取資料。

如何建立自己的contentprovider

如何建立自己的contentprovider 作為androidl四大元件 compenent activity,service,breadcasereceiver,contentprovider 之一的content provider,為其它應用程式 也可以是提供該 content provide...

安卓四大元件之ContentProvider

contentprovider是內容提供者 為什麼要有內容提供者?因為在安卓中每乙個應用的資料庫檔案都是私有的,自能在自己的應用中自己使用,別的應用要是想訪問,那麼就需要修改檔案的許可權,這樣資料就變的不安全了,所以就引入了內容提供者。首先來說一下內容提供者中的乙個重要的類uri,熟悉uri會使我們...

8 四大元件之三 ContentProvider

課程目標 理解contentprovider的作用及好處 認清contentprovider與資料儲存的關係 掌握contentprovider對外提供的資料模型形式 能夠編寫contentresolver的增刪改查 能夠自定義乙個contentprovider 能夠提供程序間訪問 了解conten...