ContentProvider 的規範編寫

2021-07-30 21:51:27 字數 1266 閱讀 5623

應用情況:與其它應用共享資料

通過 contentprovider 獲取從資料庫獲取資料

uri 用於 訪問不同的 contentprovider

如下圖:構建的路徑

下面是訪問 tasks 表的第3行資料

萬用字元的使用

使用 contact 來構建所需要的 uri ,下面內嵌的類為建立資料提供。以天氣為例

public

class

weathercontract

public

static string getsqlselectfortodayonwards() }}

1. 使用 urimathcer 來區分我們需要使用的是在哪種uri。

如 下面的 code_weather 用於查詢乙個完整的表,而code_weather_with_date 用於查詢表中的任意一行(使用的萬用字元)

public

static

final

int code_weather = 100;

public

static

final

int code_weather_with_date = 101;

private

static

final urimatcher surimatcher = buildurimatcher();

private weatherdbhelper mopenhelper;

public

static urimatcher buildurimatcher()

使用流程:

1. 擴充套件至 抽象類 contentprovider 實現 oncrate() 方法

2. 在 androidmanifest 中註冊你的內容提供器

3. 實現增刪改查等方法

4. 需要注意的是 其中又乙個 gettype 方法用於返回 固定的mime型別(字串),如的型別等。

5. 記得在進行增刪改後更新uri。

getcontext().getcontentresolver().notifychange(uri, null);

如何新增Content Provider

一 寫在前面 一直很好奇系統是如何通過 content 來訪問歸屬地資料庫的,通過研究 和api參考文件以及網路上的資料基本上弄明白了,於是有了以下的內容。二 android content provider介紹 三 content provider的組成 四 新增content provider ...

ContentProvider基本使用

在不同應用之間實現資料共享,並保證安全性 比較 新增 沒有條件約束,刪除 沒有資料contentvalues getcontentresolver insert uri uri,contentvalues values getcontentresolver update uri uri,conten...

ContentProvider簡單使用

一是使用內容提供器讀取和操作其他應用程式的資料 二是建立內容提供器給該程式的資料提供外部訪問介面 如 簿,簡訊,庫,都有內容提供器讓它們的資料有外部訪問介面 內容提供器提供的資料外部訪問介面是乙個uri.它由authority和path組成 path相當於不同的表加在authority的後面。如有表...