Android使用內容提供者方式進行儲存

2021-08-26 06:05:06 字數 1255 閱讀 8884

內容提供者(contentprovider)主要作用是對外共享資料,如果資料通過內容提供者對外共享了,那麼其他應用就可以從內容提供者中查詢到資料,並且可更新資料、刪除資料、新增資料,如果採用檔案的操作模式對外共享資料,資料的訪問方式會因為儲存方式的不同導致資料的訪問方式無法得到統一,

不同儲存方式檔案對外進行共享其訪問的api是不一樣的,如果採用內容提供者對外共享資料就會統一了資料的訪問方式。採用統一的api訪問共享的資料。

建立內容提供者步驟

1.建立內容提供者需要

繼承android.content.contentprovider

2.清單檔案中進行配置:

contentprovider類主要方法

public boolean oncreate()

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

public uriinsert(uri uri, contentvalues values)

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

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

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

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

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

public cursorquery(uri uri, stringprojection, string selection, string selectionargs, string sortorder)

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

示例:內容提供者類,實現資料的增刪改查

其他工程中訪問:

public class accesscontentproidertest extends androidtestcase public void testdelete() throws throwable public void testupdate() throws throwable public void testquery() throws throwable } }

android內容提供者

android四大元件之一,用於跨應用資料共享,我們自己的應用可以通過使用contentprovider機制獲取聯絡人資訊,簡訊,庫等資訊。不要以為只能運算元據庫資料。用getcontentresolver query insert update delete 等方法對錶進行操作 如 獲取聯絡人名字...

Android內容提供者

一 為什麼需要內容提供者元件 使用內容提供者把私有的資料庫內容暴露出來 原理 1.內容提供者把資料進行封裝,然後提供出來,其他應用都是通過內容解析者來訪問 2.定義內容提供者,定義乙個類繼承contentprovider 二 實現內容提供者步驟 1.定義乙個類繼承 contentprovider 2...

android內容提供者

內容提供者 必須在清單檔案中註冊,不需要手動執行,通過內容解決者匹配對應的uri 呼叫對應內容提供者中的增刪改查方法,在內容提供者中,事先利用匹配器,匹配一些 uri,只有這些 uri才能操作該內容提供者。1 在清單檔案中註冊 provider android name com.lmj.lianxi...