你的位置:首页 > 操作系统

[操作系统]ContentProvider(一)

注册ContentProvider:

<provider   android:name=".provider.UserProvider"  android:authorities="com.rw.contentprovider.provider.UserProvider"  android:exported="true"  ></provider>

其中authorities是URI中的域名部分,可以随便取,但必须在整个系统中唯一

      name和ACtivity一样,告诉虚拟机与之绑定的class

       exported指定ContentProvider是否允许被其他应用调用

继承ContentProvider的以下5个方法:

可以根据自己的业务需求,实现相应的方法

以下是自己写的demo,有之前写的DBOpenHelper类的支持

public Cursor query(Uri uri, String[] projection, String selection,      String[] selectionArgs, String sortOrder) {        SQLiteDatabase dataBase= this.userDatabase.getReadableDatabase();    switch (USERMATCHER.match(uri)) {    case USER:      long rawId= ContentUris.parseId(uri);      String where="uid="+rawId;      if(selection!=null){        where+=" and "+selection;      }      return dataBase.query("users", projection, where, selectionArgs, null, null,sortOrder);    case USERS:      return dataBase.query("users", projection, selection, selectionArgs, null,null, sortOrder);    default:      throw new IllegalArgumentException("this unknown Uri:"+uri.toString());    }  }

public String getType(Uri uri) {    switch (USERMATCHER.match(uri)) {    case USER:      return "vnd.android.cursor.dir/user";      case USERS:      return "vnd.android.cursor.item/user";      default:            throw new IllegalArgumentException("this is unknown Uri:"+uri.toString());    }  }

public Uri insert(Uri uri, ContentValues values) {    SQLiteDatabase dataBase= this.userDatabase.getWritableDatabase();    switch (USERMATCHER.match(uri)) {    case USER:            throw new IllegalArgumentException("this Uri can not insert a record to Content!");          case USERS:      long rawId=dataBase.insert("users", null, values);            return ContentUris.withAppendedId(uri, rawId);    default:            throw new IllegalArgumentException("this unknown Uri:"+uri.toString());    }  }

public int delete(Uri uri, String selection, String[] selectionArgs) {    SQLiteDatabase dataBase= this.userDatabase.getWritableDatabase();    switch (USERMATCHER.match(uri)) {    case USER:      long rawId= ContentUris.parseId(uri);      String where="uid="+rawId;      if(selection!=null){        where+=" and "+selection;      }      return dataBase.delete("users", where, selectionArgs);    case USERS:            return dataBase.delete("users", selection, selectionArgs);    default:            throw new IllegalArgumentException("this unknown Uri:"+uri.toString());    }  }

public int update(Uri uri, ContentValues values, String selection,      String[] selectionArgs) {    SQLiteDatabase dataBase=userDatabase.getWritableDatabase();    switch (USERMATCHER.match(uri)) {    case USER:      long rawId= ContentUris.parseId(uri);      String where="uid="+rawId;      if(selection!=null){        where+=" and "+selection;      }      return dataBase.update("users", values, where, selectionArgs);    case USERS:      if(selection!=null){        return dataBase.update("users", values, selection, selectionArgs);      }else{        throw new IllegalArgumentException("this Uri con not update record from dataBase");      }    default:            throw new IllegalArgumentException("this Uri is unknown:"+uri.toString());    }  }

在使用者使用ContentResolver对数据进行CURD操作时,分别调用了与之对应的的方法,其实其底层也是根据URI获取到了这个ContentProvider,进而将参数传递给ContentProvider中与之对应的方法。以下是其中一类的Code

public final int update(Uri uri, ContentValues values, String where,      String[] selectionArgs) {    //在这里根据传入的Uri,获取到一个ContentProvider    IContentProvider provider = acquireProvider(uri);    if (provider == null) {      throw new IllegalArgumentException("Unknown URI " + uri);    }    try {      long startTime = SystemClock.uptimeMillis();      //在这里真正执行ContentProvider的update方法      int rowsUpdated = provider.update(mPackageName, uri, values, where, selectionArgs);      long durationMillis = SystemClock.uptimeMillis() - startTime;      maybeLogUpdateToEventLog(durationMillis, uri, "update", where);      return rowsUpdated;    } catch (RemoteException e) {      // Arbitrary and not worth documenting, as Activity      // Manager will kill this process shortly anyway.      return -1;    } finally {      releaseProvider(provider);    }  }