总结自Android四大应用组件之ContentProvider篇
Android四大应用组件之Activity篇https://www.jianshu.com/p/d22981a8ad0e
Android四大应用组件之Service篇https://www.jianshu.com/p/f7d57803b86e
Android四大应用组件之ContentProvider篇https://www.jianshu.com/p/5f783f3db22d
Android四大应用组件之BroadcastReceiver篇https://www.jianshu.com/p/e6283dec21c7
ContentProvider+ContentResolver(主要修改这个)
理论概述
![](https://img.haomeiwen.com/i14355128/31964d07b05470ce.png)
![](https://img.haomeiwen.com/i14355128/e0ffd1a4a07178bf.png)
![](https://img.haomeiwen.com/i14355128/391b645b44136e2f.png)
![](https://img.haomeiwen.com/i14355128/686984ec59deef69.png)
![](https://img.haomeiwen.com/i14355128/9617002a36069be7.png)
项目练习
contentprovider:(被调用方、数据所在方)
先建个表
public class DBHelper extends SQLiteOpenHelper{
// public DBHelper(Context context,String name,CursorFactory factory,int version){
// super(context,name,factory,version);
// }
public DBHelper(Context context){
super(context,"zzz.db",null,1);
}
public void onCreate(SQLiteDatabase db){
//建表
db.execSQL("create table person(_id integer primary key autoincrement,name varchar)");
db.execSQL("insert into person (name) values ('Tom')");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
}
}
编写ContentProvider子类
class StudentContentProvider extends ContentProvider{
//用来存放所有合法uri的容器
private static UriMatcher matcher = new UriMatcher (UriMatcher.NO_MATCH);
//保存合法uri
//com.xxx.studentprovider是注册里的标识
static{
matcher.addURI("com.xxx.studentprovider","/person",1);
matcher.addURI("com.xxx.studentprovider","/person/#",2);//#任意数字
}
private DBHelper dehelper;
public boolean onCreate(){
dehelper = new DBHelper(getContext());
return false;
}
//区别对待
//content://com.xxx.studentprovider/person 不根据id查询
//content://com.xxx.studentprovider/person/3 根据id查询
public Cursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder){
// 得到连接对象
SQLiteDatabase database = dehelper.getReadableDatabase();
//匹配uri 返回code
int code = matcher.match(uri);
//合法 ---查询
if(code==1){
Cursor cursor= database.query("person",projection,selection,selectionArgs,null,null,nll);
return cursor;
}else if(code ==2){
//得到id
long id =ContentUris.parseId(uri);
//查询
Cursor cursor= database.query("person",projection,"_id=?",new String[]{id+""},null,null,nll);
//这里不可以关闭db,关了之后cursor用不了了
return cursor;
}else{//不合法 抛出异常
throw new RuntimeException("uri不合法");
}
}
//content://com.xxx.studentprovider/person 不根据id插入
//content://com.xxx.studentprovider/person/3 根据id插入(没有)
public Uri insert (Uri uri,ContentValues values){
// 得到连接对象
SQLiteDatabase database = dehelper.getReadableDatabase();
//匹配uri 返回code
int code = matcher.match(uri);
//合法 ---查询
if(code==1){
long id= database.insert("person",null,values);
//将id加到uri中
uri = ContentUris.withAppendedId(uri,id);
//这里可以关闭db,应为是插入
database.close();//应该在finally中关闭,这里简写。
return uri;
}else{//不合法 抛出异常
database.close();//应该在finally中关闭,这里简写。
throw new RuntimeException("uri不合法");
}
}
//content://com.xxx.studentprovider/person 不根据id删除
//content://com.xxx.studentprovider/person/3 根据id删除
public int delete(Uri uri,String selection,String[] selectionArgs){
// 得到连接对象
SQLiteDatabase database = dehelper.getReadableDatabase();
//匹配uri 返回code
int code = matcher.match(uri);
//合法 ---查询
int deleteCount =-1;
if(code==1){
deleteCount = database.delete("person",selection,selectionArgs);
}else if(code ==2){
//得到id
long id =ContentUris.parseId(uri);
//删除
deleteCount = database.delete("person","_id="+id,null);
}else{//不合法 抛出异常
database.close();
throw new RuntimeException("uri不合法");
}
database.close();
return deleteCount ;
}
//content://com.xxx.studentprovider/person 不根据id更新
//content://com.xxx.studentprovider/person/3 根据id更新
public int update(Uri uri,ContentValues values,String selection,String[] selectionArgs){
// 得到连接对象
SQLiteDatabase database = dehelper.getReadableDatabase();
//匹配uri 返回code
int code = matcher.match(uri);
//合法 ---查询
int updateCount =-1;
if(code==1){
updateCount = database.update("person",values,selection,selectionArgs);
}else if(code ==2){
//得到id
long id =ContentUris.parseId(uri);
//删除
updateCount = database.update("person",values,"_id="+id,null);
}else{//不合法 抛出异常
database.close();
throw new RuntimeException("uri不合法");
}
database.close();
return updateCount ;
}
public String getType(Uri uri){
return null;
}
}
在manifest.xml中注册
<provider
android:name=".StudentContentProvider"
android:authorities="com.xxx.studentprovider"//标识名称,一般为全类名小写
android:exported="true"//别的应用访问开关
/>
一旦注册上就会创建好 、初始化。
MainActivity里的contentresolver:
public void query(){
//得到contentresolver对象
ContentResolver resolver =getContentResolver();
//调用query 得到cursor
Uri uri = Uri.parse("content://com.xxx.studentprovider/person/");
Cursor cursor=resolver.query(uri,null,null,null,null);
//取出cursor中的数据
while(cursor.moveToNext()){
int id = cursor.getInt(0);
String name = cursor.getString(1);
Toast.makeText(this,id+":"+name,1).show();
}
cursor.close();
}
public void insert(){
//得到contentresolver对象
ContentResolver resolver =getContentResolver();
//调用insert
Uri uri = Uri.parse("content://com.xxx.studentprovider/person/");
ContentValues.put("name","JACK");
uri = resolver.insert(uri,values);
Toast.makeText(this,uri.toString(),0).show();
}
public void update(){
//得到contentresolver对象
ContentResolver resolver =getContentResolver();
//调用update
Uri uri = Uri.parse("content://com.xxx.studentprovider/person/2");
ContentValues.put("name","JACK2");
int updateCount = resolver.update(uri,values,null,null);
Toast.makeText(this,"updateCount ="+updateCount ,1).show();
}
public void delete(){
//得到contentresolver对象
ContentResolver resolver =getContentResolver();
//调用update
Uri uri = Uri.parse("content://com.xxx.studentprovider/person/2");
ContentValues.put("name","JACK2");
int deleteCount = resolver.delete(uri,null,null);
Toast.makeText(this,"deleteCount ="+deleteCount ,1).show();
}
网友评论