內容提供者ContentProvider的使用詳解

2021-07-08 20:14:48 字數 3554 閱讀 9947



首先,contentprovider(內容提供者)是android中的四大元件之一,但是在一般的開發中,可能使用的比較少。

contentprovider為不同的軟體之間資料共享,提供統一的介面。也就是說,如果我們想讓其他的應用使用我們自己程式內的資料,就可以使用contentprovider定義乙個對外開放的介面,從而使得其他的應用可以使用咱們應用的檔案、資料庫內儲存的資訊。當然,自己開發的應用需要給其他應用共享資訊的需求可能比較少見,但是在android系統中,很多系統自帶應用,比如聯絡人資訊,庫,音訊庫等應用,為了對其他應用暴露資料,所以就使用了contentprovider機制。所以,我們還是要學習contentprovider的基本使用,在遇到獲取聯絡人資訊,庫,音訊庫等需求的時候,才能更好的實現功能

android系統為了讓我們更好的對外暴露資料,提供了統一的介面,所以定義了抽象類contentprovider,因此,如果我們想對外提供資料,我們需要繼承contentprovider,並且實現下面的這幾個方法:

oncreate()

當我們的provider初始化時被呼叫,我們應該在這個方法裡面完成部分初始化操作

query()查詢方法,用於給呼叫者返回資料

insert()插入操作,用於讓外部應用插入資料到內容提供者中

update()更新操作,用於更新內容提供者的資料

delete()用於刪除資料

gettype返回內容提供者的mime type

上面這些方法,當我們繼承自contentprovider的時候,eclipse會自動的給我們新增,但是這並不代表我們每個方法都需要自定義實現。如果我們只希望給其他應用提供資料,而不允許其他應用修改我們的資料,那麼我們只需要實現oncreate(),gettype()和query()這三個方法就可以了,其他的三個方法我們可以根據業務需求,實現或者是不實現。

因為一般使用contentprovider向外部暴露資料庫的資訊,因此,本篇將以使用contentprovider向其他應用暴露資料庫資訊為例,講解contentprovider的基本使用。

android中sqlite資料庫的建立和使用,本篇不再介紹,不清楚的請看這篇文章

sqlite資料庫的簡單實用

假設讀者已經學會了sqlite資料庫的使用,並且已經建立好了資料庫,下面我們開始寫我們的contentprovider。

因為注釋解析的比較詳細,所以就不過多解釋了

/**

* 內容提供者

* * @author zhaokaiqiang

* @time 2023年6月6日

*/public class studentprovider extends contentprovider

// 進行資料的初始化操作

@override

public boolean oncreate()

// 查詢

// 如果uri為 content:

// 則代表查詢所有的student表內的資料

// 如果uri為 content:/6

// 則代表查詢student表內id=6的資料

@override

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

return db.query("student", projection, where, selectionargs, null, null, sortorder);

//如uri不匹配,丟擲不合法引數的異常

default:

throw new illegalargumentexception("unkwon uri:" + uri.tostring());

} }// 插入

@override

public uri insert(uri uri, contentvalues values)

} //刪除資料

@override

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

count = db.delete("student", where, selectionargs);

return count;

default:

throw new illegalargumentexception("unkwon uri:" + uri.tostring());

} }//更新資料

@override

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

count = db.update("student", values, where, selectionargs);

return count;

default:

throw new illegalargumentexception("unkwon uri:" + uri.tostring());

} }// 用於獲取mime type

@override

public string gettype(uri uri)

}}

我們在定義好我們的contentprovider之後,因為

contentprovider資料四大元件之一,因此我們還需要在androidmanifest清單檔案中進行註冊才能使用,下面是註冊資訊

至此,我們就完成了我們自己的contentprovider的生命,其他的應用現在就可以使用我們往外部暴露的資料資訊了。

我們已經定義好了我們自己的contentprovider,那麼外部應用如何呼叫呢?

下面,我將新建乙個測試單元工程,完成對contentprovider的各個方法的測試

新增方法測試

//使用contentprovider新增資料的測試

public void testadd() throws throwable

刪除方法測試

//使用contentprovider刪除資料的測試

public void testdelete() throws throwable

修改方法測試

//使用contentprovider更新資料的測試

public void testupdate() throws throwable

//使用contentprovider查詢資料的測試

public void testfind() throws throwable

}

上面的方法都經過了單元測試。

好了,至此,我們就使用contentprovider實現了在第三方應用中對我們應用的資料庫進行增刪改查等操作

內容提供者

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...