Android ContentProvider
程序员文章站
2024-02-14 15:23:16
...
- ContentProvider,Android四大组件之一。可以实现进程间数据共享。
有3个辅助类
- ContentUris类,用来在Uri后追加id、获取id。
- UriMatcher类 ,用来注册ContentProvider的Uri,根据提供的Uri匹配ContentProvider中对应的表。
- ContentObserver,内容观察者,指定Uri的ContentProvider的内容发送改变,并通知注册了观察者。
操作类
ContentResolver抽象类,用来管理所有的ContentProvider。通过Uri来确定ContentProvider。有增删改查的方法。
示例
数据源SQLite数据库
public class DBOpenHelper extends SQLiteOpenHelper {
/**
* 数据库的名字
*/
private static final String DB_NAME = "customer.db";
/**
* 版本号
*/
private static int sDBVersion = 1;
/**
* 表名
*/
public static final String TABLE_MAN_NAME = "man";
public static final String TABLE_DOG_NAME = "dog";
public DBOpenHelper(Context context) {
super(context, DB_NAME, null, sDBVersion);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists " + TABLE_MAN_NAME + "(_id integer primary key autoincrement,name text)");
db.execSQL("create table if not exists " + TABLE_DOG_NAME + "(_id integer primary key autoincrement,name text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
自定义ContentProvider
public class MyContentProvider extends ContentProvider {
private DBOpenHelper dbOpenHelper = null;
private SQLiteDatabase db = null;
public static final String AUTOHORITY = "com.example.study.contentprovider";
public static final int CODE_MAN = 1;
public static final int CODE_DOG = 2;
static UriMatcher sUriMatcher = null;
static {
sUriMatcher = new UriMatcher(NO_MATCH);
//权限
//路径,可以是任何值
//匹配的时候会返回的code
sUriMatcher.addURI(AUTOHORITY, "man", CODE_MAN);
sUriMatcher.addURI(AUTOHORITY, "dog", CODE_DOG);
}
@Override
public boolean onCreate() {
dbOpenHelper = new DBOpenHelper(getContext());
db = dbOpenHelper.getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
String tableName = getTableName(uri);
Cursor query = db.query(tableName, null, null, null, null, null, null);
query.moveToFirst();
return query;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
String tableName = getTableName(uri);
db.insert(tableName, null, values);
Log.i("name",Thread.currentThread().getName());
getContext().getContentResolver().notifyChange(uri,null);
return uri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
String tableName = getTableName(uri);
db.delete(tableName, selection, selectionArgs);
return 0;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
String tableName = getTableName(uri);
db.update(tableName, values, selection, selectionArgs);
return 0;
}
/**
* 根据URI匹配 URI_CODE,从而匹配ContentProvider中相应的表名
*/
private String getTableName(Uri uri) {
String tableName = null;
switch (sUriMatcher.match(uri)) {
case CODE_MAN:
tableName = DBOpenHelper.TABLE_MAN_NAME;
break;
case CODE_DOG:
tableName = DBOpenHelper.TABLE_DOG_NAME;
break;
default:
tableName=DBOpenHelper.TABLE_MAN_NAME;
}
return tableName;
}
}
Activity中
private ContentResolver contentResolver;
private Uri uri;
private Cursor query;
private ContentValues values;
private ContentObserver contentObserver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_provider);
ButterKnife.bind(this);
contentResolver = getContentResolver();
contentObserver = new ContentObserver(new Handler()) {
@Override
public boolean deliverSelfNotifications() {
Log.i("cp", "deliverSelfNotifications");
return true;
}
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
Log.i("cp", "onChange--- selfChange:"+selfChange);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
Log.i("cp", "onChange--- selfChange:"+selfChange+" uri:"+uri.toString());
}
};
uri = Uri.parse("content://com.example.study.contentprovider/man");
//注册内容观察者
contentResolver.registerContentObserver(uri, false,contentObserver );
}
...
//点击
case R.id.add_man:
uri = Uri.parse("content://com.example.study.contentprovider/man");
insert(uri, new ContentValues(), "小明");
break;
case R.id.query_man:
uri = Uri.parse("content://com.example.study.contentprovider/man");
query = contentResolver.query(uri, null, null, null, null, null);
while (query.moveToNext()) {
int id = query.getInt(0);
String name = query.getString(1);
String s = " id:" + id + " name:" + name;
buffer.append(s + "\n");
Log.i("cp", s);
}
break;
case R.id.delete_man:
uri = Uri.parse("content://com.example.study.contentprovider/man");
contentResolver.delete(uri, null, null);
break;
case R.id.add_dog:
uri = Uri.parse("content://com.example.study.contentprovider/dog");
insert(uri, new ContentValues(), "秋田");
break;
case R.id.query_dog:
uri = Uri.parse("content://com.example.study.contentprovider/dog");
query = contentResolver.query(uri, null, null, null, null, null);
while (query.moveToNext()) {
int id = query.getInt(0);
String name = query.getString(1);
String s = " id:" + id + " name:" + name;
buffer.append(s + "\n");
Log.i("cp", s);
}
break;
case R.id.delete_dog:
uri = Uri.parse("content://com.example.study.contentprovider/dog");
contentResolver.delete(uri, null, null);
break;
....
...
if (null != query) {
query.close();
query=null;
}
上面的点击进行了添加,查询,删除的操作。
总结
四大组件之一。
夸进程通信。数据共享。
原理是Binder机制
线程池处理任务,需要考虑线程安全。
推荐阅读
-
Android ContentProvider
-
android R启动找不到super分区问题
-
Android studio 中找不到 R
-
Android ContentProvider
-
com.android.internal.R 错误: 找不到符号
-
Android LayoutInflater深入分析及应用
-
Android4.4 Uri获取图片及document理解
-
Android编程使用android-support-design实现MD风格对话框功能示例
-
androidpn(安卓信息推送工具) 博客分类: 移动开发 android
-
SQLite外键(Foreign Key)支持 博客分类: android SQLiteSinatraUbuntuDebianSQL