ANDROID平臺中實現(xiàn)數(shù)據(jù)共享的機制CONTENT PROVIDER.doc_第1頁
ANDROID平臺中實現(xiàn)數(shù)據(jù)共享的機制CONTENT PROVIDER.doc_第2頁
ANDROID平臺中實現(xiàn)數(shù)據(jù)共享的機制CONTENT PROVIDER.doc_第3頁
全文預覽已結束

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

此文檔收集于網(wǎng)絡,如有侵權,請聯(lián)系網(wǎng)站刪除Android平臺中實現(xiàn)數(shù)據(jù)共享的機制Content ProviderContentProvider是安卓平臺中,在不同應用程序之間實現(xiàn)數(shù)據(jù)共享的一種機制。一個應用程序如果需要讓別的程序可以操作自己的數(shù)據(jù),即可采用這種機制。并且此種方式忽略了底層的數(shù)據(jù)存儲實現(xiàn),ContentProvider提供了一種統(tǒng)一的通過Uri實現(xiàn)數(shù)據(jù)操作的方式。其步驟為: 1. 在當前應用程序中定義一個ContentProvider。 2. 在當前應用程序的AndroidManifest.xml中注冊此ContentProvider 3. 其他應用程序通過ContentResolver和Uri來獲取此ContentProvider的數(shù)據(jù)。 ContentResolver提供了諸如insert(), delete(), query()和update()之類的方法,用于實現(xiàn)對ContentProvider中數(shù)據(jù)的存取操作。 Uri是一個通用資源標志符,將其分為A,B,C,D 4個部分: A:無法改變的標準前綴,包括;content:/、tel:/等。當前綴是content:/時,說明通過一個Content Provider控制這些數(shù)據(jù) B:URI的標識,它通過authorities屬性聲明,用于定義了是哪個ContentProvider提供這些數(shù)據(jù)。對于第三方應用程序,為了保證URI標識的唯一性,它必須是一個完整的、小寫的 類名。例如;content:/com.silent.data.myprovider C:路徑,可以近似的理解為需要操作的數(shù)據(jù)庫中表的名字,如:content:/hx.android.text.myprovider/name中的name D:如果URI中包含表示需要獲取的記錄的ID;則就返回該id對應的數(shù)據(jù),如果沒有ID,就表示返回全部; 下面通過是代碼示例,演示一下如何在應用之間相互獲取數(shù)據(jù)。 在應用程序A中,繼承ContProvider類,并重寫其中方法。 Java代碼 收藏代碼public class MyProvider extends ContentProvider Override public int delete(Uri uri, String selection, String selectionArgs) / TODO Auto-generated method stub return 0; Override public String getType(Uri uri) / TODO Auto-generated method stub return null; Override public Uri insert(Uri uri, ContentValues values) return null; /在Create中初始化一個數(shù)據(jù)庫 Override public boolean onCreate() SQLiteDatabase db = this.getContext().openOrCreateDatabase(silent.db3, Context.MODE_PRIVATE, null); db.execSQL(create table tab(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL); ContentValues values = new ContentValues(); values.put(name, test); db.insert(tab, _id, values); db.close(); return true; /實現(xiàn)query方法 Override public Cursor query(Uri uri, String projection, String selection, String selectionArgs, String sortOrder) SQLiteDatabase db = this.getContext().openOrCreateDatabase(silent.db3, Context.MODE_PRIVATE, null); Cursor c = db.query(tab, null, null, null, null, null,null); return c; Override public int update(Uri uri, ContentValues values, String selection, String selectionArgs) / TODO Auto-generated method stub return 0; 在其AndroidManifest.xml中聲明此ContentProvider,其中authorities屬性定義了此ContentProvider的Uri標識。 Java代碼 收藏代碼 在應用程序B中,通過ContentResolver獲取程序A的ContentProvider中的數(shù)據(jù)。 Java代碼 收藏代碼Java代碼 public class MainActivity extends Activity Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState); setContentView(R.layout.main); /獲取上下文 Context ctx = MainActivity.this; /獲取ContentResolver對象 ContentResolver resolver = ctx.getContentResolver(); /獲取Uri對象 Uri uri = Uri.parse(content:/com.silent.MyProvider); /獲取數(shù)據(jù) Cursor c = resolver.query(uri, null, null, null, null); c.moveToFirst(); for(int i=0; ic.getCount(); i+) int index = c.getColumnIndexOrThrow(name); String src = c.getString(index); Log.d(, src); c.moveToNext(); 以上就是ContentProvider的使用方式,這種存儲方式相比SQLite和SharedPref

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論