一、Content Provider 增删查改
Cursor cursor=getContentResolver().query(
uri,
projection,
selection,
selectionArgs,
sortOrder);
//查询
if(cursor!=null){
while(cursor.moveToNext()){
String column1=cursor.getString(cursor.getColumnIndex("column1"));
int coloumn2=cursor.getInt(cursor.getColumnIndex("column2"));
}
cursor.close();
}
//插入
ContentValues values=new ContentValues();
values.put("column1", "");
values.put("column2", 2);
//更新
ContentValues values=new ContentValues();
values.put("column1", "");
getContentResolver().update(uri, values, "column1=? and column2=?", new String[]{"text","2"});
//删除
getContentResolver().delete(url, "column2=?",
new String[]{"1"});
二、获取联系人应用内容(实例)
private void readContacts() {
Cursor cursor=null;
try {
cursor=getContentResolver().query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null, null, null, null); //获取联系人应用中的信息
while(cursor.moveToNext()){
String name=cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=cursor.getString(cursor.getColumnIndex
(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactLists.add(name+"\n"+number);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(cursor!=null){
cursor.close();
}
}
}