亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android編程使用內(nèi)容提供者方式(ContentProvider)進行存儲的方法

 更新時間:2016年01月04日 11:51:26   作者:傅榮康  
這篇文章主要介紹了Android編程使用內(nèi)容提供者方式進行存儲的方法,涉及Android內(nèi)容提供者的創(chuàng)建,配置及針對數(shù)據(jù)的增刪改查等操作技巧,需要的朋友可以參考下

本文實例講述了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類主要方法

復制代碼 代碼如下:
public boolean onCreate()

該方法在ContentProvider創(chuàng)建后就會被調(diào)用, Android開機后, ContentProvider在其它應(yīng)用第一次訪問它時才會被創(chuàng)建。
復制代碼 代碼如下:
public Uriinsert(Uri uri, ContentValues values)

該方法用于供外部應(yīng)用往ContentProvider添加數(shù)據(jù)。
復制代碼 代碼如下:
public int delete(Uri uri, String selection,String[] selectionArgs)

該方法用于供外部應(yīng)用從ContentProvider刪除數(shù)據(jù)。
復制代碼 代碼如下:
public int update(Uri uri, ContentValues values, Stringselection, String[] selectionArgs)

該方法用于供外部應(yīng)用更新ContentProvider中的數(shù)據(jù)。
復制代碼 代碼如下:
public Cursorquery(Uri uri, String[]projection, String selection, String[] selectionArgs, String sortOrder)

該方法用于供外部應(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)文章

最新評論