http://sishuok.com/forum/blogPost/list/0/2749.html#7733
public static final String URI_AUTHORITY = "cn.javass.mycp"; public static final String URI_PATH = "Users"; public static final String URI_PATH2 = "Users/#"; public static final Uri CONTENT_URI = Uri.parse("content://" + URI_AUTHORITY + "/" + URI_PATH); //对外的数据字段 public static final String COLUMN_UUID = "uuid"; public static final String COLUMN_NAME = "name";
public static final int ALL_RECORDS = 1; public static final int SINGLE_RECORD = 2; public static UriMatcher sMatcher = null; static { sMatcher = new UriMatcher(UriMatcher.NO_MATCH); sMatcher.addURI(URI_AUTHORITY, URI_PATH, ALL_RECORDS); sMatcher.addURI(URI_AUTHORITY, URI_PATH2, SINGLE_RECORD); }
public Uri insert(Uri uri, ContentValues values) { Uri retUri = null; if(sMatcher.match(uri)==ALL_RECORDS){ //判断是否需要处理,只有符合的才处理 SQLiteDatabase db = dh.getWritableDatabase(); long id = db.insert("tbl_user",null, values); retUri = ContentUris.withAppendedId(uri, id); } return retUri; }
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { if(sMatcher.match(uri)==ALL_RECORDS){ SQLiteDatabase db = dh.getWritableDatabase(); Cursor c = db.query("tbl_user", projection, selection,selectionArgs, "", "", sortOrder,""); return c; }else if(sMatcher.match(uri)==SINGLE_RECORD){ //这里应该处理带id的uri,省略了..... } return null; }
public int update(Uri uri, ContentValues values, String selection,String[] selectionArgs) { int ret = 0; if(sMatcher.match(uri)==ALL_RECORDS){ //判断是否需要处理,只有符合的才处理 SQLiteDatabase db = dh.getWritableDatabase(); ret = db.update("tbl_user",values,selection, selectionArgs); }else if(sMatcher.match(uri)==SINGLE_RECORD){//这里应该处理带id的uri,省略了.....} return ret; }
public int delete(Uri uri, String selection, String[] selectionArgs) { int ret = 0; if(sMatcher.match(uri)==ALL_RECORDS){ //判断是否需要处理,只有符合的才处理 SQLiteDatabase db = dh.getWritableDatabase(); ret = db.delete("tbl_user", selection, selectionArgs); }else if(sMatcher.match(uri)==SINGLE_RECORD){//这里应该处理带id的uri,省略了.....} return ret; }
public String getType(Uri uri) { switch (sMatcher.match(uri)) { case ALL_RECORDS: return "vnd.android.cursor.dir/vnd.cn.javass.users"; case SINGLE_RECORD: return "vnd.android.cursor.item/vnd.cn.javass.users"; default: throw new IllegalArgumentException("unknown URI " + uri); } }
public boolean onCreate() { if (mContext == null) {mContext = this.getContext();} if(dh==null){ dh = new DBHelper(mContext,"testDB1",null,1); } return true; }
<provider android:name=".MyCP" android:authorities="cn.javass.mycp"></provider>
ContentValues values = new ContentValues(); values.put(MyCP.COLUMN_UUID, "test1"); values.put(MyCP.COLUMN_NAME,"cc1"); Uri uri = getContentResolver().insert(MyCP.CONTENT_URI, values);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(MyCP.CONTENT_URI) .withValue(MyCP.COLUMN_UUID, "test2") .withValue(MyCP.COLUMN_NAME, "cc2") .build()); ops.add(ContentProviderOperation.newInsert(MyCP.CONTENT_URI) .withValue(MyCP.COLUMN_UUID, "test3") .withValue(MyCP.COLUMN_NAME, "cc3") .build()); try { getContentResolver().applyBatch(MyCP.URI_AUTHORITY,ops); } catch (Exception e) { e.printStackTrace(); }
Cursor c = getContentResolver().query(MyCP.CONTENT_URI, new String[] {MyCP.COLUMN_UUID, MyCP.COLUMN_NAME}, null,null, null); while (c.moveToNext()) { String dId = ""+c.getString(c.getColumnIndex(MyCP.COLUMN_UUID)); String name = c.getString(c.getColumnIndex(MyCP.COLUMN_NAME)); Log.i("now query","dataId="+dId+",name="+name); }
ContentValues values = new ContentValues(); values.put(MyCP.COLUMN_UUID, "test1"); values.put(MyCP.COLUMN_NAME,"cc1update"); getContentResolver().update(MyCP.CONTENT_URI, values,MyCP.COLUMN_UUID+"=?",new String[]{"test1"});
getContentResolver().delete(MyCP.CONTENT_URI, MyCP.COLUMN_UUID + "=?",new String[] { "test2" });
getContext().getContentResolver().notifyChange(Uri, null);
<uses-permission android:name="android.permission.WRITE_CONTACTS"/> <uses-permission android:name="android.permission.READ_CONTACTS"/>
//1:添加原始的帐号信息 ContentValues values = new ContentValues(); values.put(RawContacts.ACCOUNT_TYPE, "userAccount"); values.put(RawContacts.ACCOUNT_NAME, "cc"); Uri rawContactUri = getContentResolver().insert(RawContacts.CONTENT_URI, values); long rawContactId = ContentUris.parseId(rawContactUri); //2:添加账户人员的姓名 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE); values.put(StructuredName.DISPLAY_NAME, "cc1Name"); getContentResolver().insert(Data.CONTENT_URI, values);
//3:添加电话信息 values.clear(); values.put(Data.RAW_CONTACT_ID, rawContactId); values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, "13567890987"); values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, "cc1"); Uri dataUri = getContentResolver().insert(Data.CONTENT_URI,values); //同理,还可以添加Email等等信息
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>(); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, rawContactId) .withValue(Data.MIMETYPE, StructuredName.CONTENT_ITEM_TYPE) .withValue(StructuredName.DISPLAY_NAME, "cc1Name") .build()); ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI) .withValue(Data.RAW_CONTACT_ID, rawContactId) .withValue(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE) .withValue(Phone.NUMBER, "13567890989") .withValue(Phone.TYPE, Phone.TYPE_CUSTOM) .withValue(Phone.LABEL, "cc1").build()); try { getContentResolver().applyBatch(ContactsContract.AUTHORITY,ops); } catch (Exception e) { e.printStackTrace(); }
//1:得到要操作的原始帐号信息 Cursor c = getContentResolver().query(RawContacts.CONTENT_URI, new String[]{RawContacts._ID},RawContacts.ACCOUNT_NAME+"=? ",new String[]{"cc"}, null); long rawContactId = 0L; while(c.moveToNext()){ rawContactId = c.getLong(c.getColumnIndex(RawContacts._ID)); } c.close(); //然后开始获取你需要的数据,这里示范读取电话数据,同理可以读取其他的数据,如Email数据 c = getContentResolver().query(Data.CONTENT_URI, new String[] {Data._ID, Phone.NUMBER, Phone.TYPE, Phone.LABEL}, Data.RAW_CONTACT_ID+"=?"+" and "+Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'", new String[]{""+rawContactId}, null); while (c.moveToNext()) { String dId = ""+c.getInt(c.getColumnIndex(Data._ID)); String name = c.getString(c.getColumnIndex(Phone.LABEL)); String num = c.getString(c.getColumnIndex(Phone.NUMBER)); String type = c.getString(c.getColumnIndex(Phone.TYPE)); Log.i("now query","dataId="+dId+",name="+name+",name="+num+", type="+type); }
//1:应该要先得到要修改的Data数据,这里为了示范简单,就直接改了 //2:设置修改的值 ContentValues values = new ContentValues(); values.put(Data.RAW_CONTACT_ID, 3);//测试时的数据是3 values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE); values.put(Phone.NUMBER, “13567890981”);//修改了 values.put(Phone.TYPE, Phone.TYPE_CUSTOM); values.put(Phone.LABEL, “upuser1”);//修改了 getContentResolver().update(Data.CONTENT_URI, values, Data._ID + "=?", new String[] { “31” });//测试时的id是31
//1:应该要先得到要删除的Data数据,这里为了示范简单,就直接改了 //2:删除数据,直接删除原始的内容 getContentResolver().delete(RawContacts.CONTENT_URI, Data._ID + "=?", new String[] { "31" });