Android編程使用內(nèi)容提供者方式(ContentProvider)進行存儲的方法
本文實例講述了Android編程使用內(nèi)容提供者方式進行存儲的方法。分享給大家供大家參考,具體如下:
內(nèi)容提供者(ContentProvider)主要作用是對外共享數(shù)據(jù),如果數(shù)據(jù)通過內(nèi)容提供者對外共享了,那么其他應(yīng)用就可以從內(nèi)容提供者中查詢到數(shù)據(jù),并且可更新數(shù)據(jù)、刪除數(shù)據(jù)、添加數(shù)據(jù),如果采用文件的操作模式對外共享數(shù)據(jù),數(shù)據(jù)的訪問方式會因為存儲方式的不同導致數(shù)據(jù)的訪問方式無法得到統(tǒng)一,不同存儲方式文件對外進行共享其訪問的ApI是不一樣的,如果采用內(nèi)容提供者對外共享數(shù)據(jù)就會統(tǒng)一了數(shù)據(jù)的訪問方式。采用統(tǒng)一的API訪問共享的數(shù)據(jù)。
創(chuàng)建內(nèi)容提供者步驟
1.創(chuàng)建內(nèi)容提供者需要繼承android.content.ContentProvider
2.清單文件中進行配置:
<!--android:name:指定內(nèi)容提供者的類名,android:authorities:調(diào)用內(nèi)容..名稱,隨意取 -->
<provider android:name=".PersonProvider" android:authorities="cn.test.providers.personprovider"/>
ContentProvider類主要方法
該方法在ContentProvider創(chuàng)建后就會被調(diào)用, Android開機后, ContentProvider在其它應(yīng)用第一次訪問它時才會被創(chuàng)建。
該方法用于供外部應(yīng)用往ContentProvider添加數(shù)據(jù)。
該方法用于供外部應(yīng)用從ContentProvider刪除數(shù)據(jù)。
該方法用于供外部應(yīng)用更新ContentProvider中的數(shù)據(jù)。
該方法用于供外部應(yīng)用從ContentProvider中獲取數(shù)據(jù)。
示例:
內(nèi)容提供者類,實現(xiàn)數(shù)據(jù)的增刪改查
public class PersonProvider extends ContentProvider { //創(chuàng)建工具類實現(xiàn)Uri匹配 private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH); private static final int PERSONS = 1; private static final int PERSON = 2; static{ MATCHER.addURI("cn.test.providers.personprovider", "person", PERSONS); MATCHER.addURI("cn.test.providers.personprovider", "person/#", PERSON); } private DBOpenHelper dbOpenHelper = null; @Override public boolean onCreate() { dbOpenHelper = new DBOpenHelper(getContext()); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: // 獲取person表中的所有數(shù)據(jù) /person return db.query("person", projection, selection, selectionArgs, null, null, sortOrder); case PERSON: // 獲取person表中的指定id的數(shù)據(jù) /person/20 long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } return db.query("person", projection, where, selectionArgs, null, null, sortOrder); default: throw new IllegalArgumentException("Unknown Uri:"+ uri); } } @Override public String getType(Uri uri) { // TODO Auto-generated method stub return null; } @Override public Uri insert(Uri uri, ContentValues values) { //取得數(shù)據(jù)庫操作實例 SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); switch (MATCHER.match(uri)) { case PERSONS: //執(zhí)行添加,返回行號,如果主健字段是自增長的,那么行號會等于主鍵id long rowid = db.insert("person", "name", values); //得到拼好的uri Uri insertUri = ContentUris.withAppendedId(uri, rowid); //發(fā)出數(shù)據(jù)變化通知(person表的數(shù)據(jù)發(fā)生變化) getContext().getContentResolver().notifyChange(uri, null); return insertUri; default: //不能識別uri throw new IllegalArgumentException("Unknown Uri:"+ uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); //受影響的行數(shù) int num = 0; switch (MATCHER.match(uri)) { case PERSONS: // 刪除person表中的所有數(shù)據(jù) /person num = db.delete("person", selection, selectionArgs); break; case PERSON: // 刪除person表中的指定id的數(shù)據(jù) /person/20 long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } num = db.delete("person", where, selectionArgs); break; default: throw new IllegalArgumentException("Unknown Uri:"+ uri); } return num; } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); int num = 0; switch (MATCHER.match(uri)) { case PERSONS: // 更新person表中的所有數(shù)據(jù) /person num = db.update("person", values, selection, selectionArgs); break; case PERSON: // 更新person表中的指定id的數(shù)據(jù) /person/20 long id = ContentUris.parseId(uri); String where = "personid="+ id; if(selection!=null && !"".equals(selection.trim())){ where += " and "+ selection; } num = db.update("person", values, where, selectionArgs); break; default: throw new IllegalArgumentException("Unknown Uri:"+ uri); } return num; } }
其他工程中訪問:
public class AccessContentProiderTest extends AndroidTestCase { public void testInsert() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person"); ContentValues values = new ContentValues(); values.put("name", "lili"); values.put("phone", "110"); values.put("amount", "3000000000"); resolver.insert(uri, values); } public void testDelete() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person"); int num =resolver.delete(uri, null, null); } public void testUpdate() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person/65"); ContentValues values = new ContentValues(); values.put("amount", 500); resolver.update(uri, values, null, null); } public void testQuery() throws Throwable{ ContentResolver resolver = getContext().getContentResolver(); Uri uri = Uri.parse("content://cn.test.providers.personprovider/person/65"); Cursor cursor = resolver.query(uri, null, null, null, "personid asc"); while(cursor.moveToNext()){ String name = cursor.getString(cursor.getColumnIndex("name")); Log.i("AccessContentProviderTest", name); } } }
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實現(xiàn)代碼
本篇文章對Android中監(jiān)聽系統(tǒng)網(wǎng)絡(luò)連接打開或者關(guān)閉的實現(xiàn)用實例進行了介紹。需要的朋友參考下2013-05-05Android.permission.MODIFY_PHONE_STATE權(quán)限問題解決辦法
這篇文章主要介紹了Android.permission.MODIFY_PHONE_STATE權(quán)限問題解決辦法的相關(guān)資料,這里提供了幾種方法幫助大家解決這種問題,需要的朋友可以參考下2016-12-12Android中webview與JS交互、互調(diào)方法實例詳解
這篇文章主要介紹了Android中webview與JS交互、互調(diào)方法實例詳解的相關(guān)資料,需要的朋友可以參考下2017-03-03Kotlin協(xié)程Dispatchers原理示例詳解
這篇文章主要為大家介紹了Kotlin協(xié)程Dispatchers原理示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08Android DatePicker和DatePickerDialog基本用法示例
這篇文章主要介紹了Android DatePicker和DatePickerDialog基本用法,實例分析了DatePicker和DatePickerDialog控件針對手機時間設(shè)置的相關(guān)技巧,需要的朋友可以參考下2016-06-06Android Studio 3.5格式化布局代碼時錯位、錯亂bug的解決
這篇文章主要介紹了Android Studio 3.5格式化布局代碼時錯位、錯亂bug的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android 自定義 HorizontalScrollView 打造多圖片OOM 的橫向滑動效果(實例代碼)
這篇文章主要介紹了Android 自定義 HorizontalScrollView 打造多圖片OOM 的橫向滑動效果(實例代碼),需要的朋友可以參考下2017-10-10