Android共享数据ContentProvider的使用
2014-07-06 15:44
483 查看
本文来自:http://yaku2688.iteye.com/blog/1185364
ContentProvider数据共享
1.首先在AndroidManifest.xml文件中添加对外暴露的数据共享接口Content
Xml代码
<provider android:name=".UserProvider" android:authorities="com.yaku.ContentProvider.userprovider"/>
ContentProvider采用了authorities(主机名/域名)对它进行唯一标识,authorities 就是他的域名
2.Url解析
content://com.yaku.ContentProvider.userprovider/user/2
【content://】 Android定义的内容提供都的Schema
【com.yaku.ContentProvider.userprovider】 主机名或者authorities
【user】 路径
【2】 ID
示例代码:
数据结构User.java:
Java代码
package com.yaku.pojo;
public class User {
private int id;
private String name;
private int age;
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [age=" + age + ", id=" + id + ", name=" + name + "]";
}
}
数据库操作DBOpenHelper.java:
Java代码
package com.yaku.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DBNAME = "yaku.db"; //数据库名称
private static final int DBVER = 1;//数据库版本
public DBOpenHelper(Context context) {
super(context, DBNAME, null, DBVER);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE user (userid integer primary key autoincrement, name varchar(20), age integer)";
db.execSQL(sql);//执行有更改的sql语句
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS user");
onCreate(db);
}
}
对外共享处理类ContentProviderUser.java:
Java代码
package com.yaku.ContentProvider;
import com.yaku.db.DBOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class ContentProviderUser extends ContentProvider {
private DBOpenHelper dbOpenHelper;
//常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int USERS = 1;
private static final int USER = 2;
static{
//如果match()方法匹配content://com.yaku.ContentProvider.userprovider/user路径,返回匹配码为1
MATCHER.addURI("com.yaku.ContentProvider.userprovider", "user", USERS);
//如果match()方法匹配content://com.yaku.ContentProvider.userprovider/user/123路径,返回匹配码为2
MATCHER.addURI("com.yaku.ContentProvider.userprovider", "user/#", USER);//#号为通配符
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case USERS:
count = db.delete("user", selection, selectionArgs);
return count;
case USER:
//ContentUris类用于获取Uri路径后面的ID部分
long id = ContentUris.parseId(uri);
String where = "userid = "+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.delete("user", where, selectionArgs);
return count;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
/**
* 该方法用于返回当前Url所代表数据的MIME类型。
* 如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头
* 如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头
*/
@Override
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case USERS:
return "vnd.android.cursor.dir/user";
case USER:
return "vnd.android.cursor.item/user";
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case USERS:
long rowid = db.insert("user", "name", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri
this.getContext().getContentResolver().notifyChange(uri, null);
return insertUri;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public boolean onCreate() {
this.dbOpenHelper = new DBOpenHelper(this.getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case USERS:
return db.query("user", projection, selection, selectionArgs, null, null, sortOrder);
case USER:
long id = ContentUris.parseId(uri);
String where = "userid = "+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
return db.query("user", projection, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case USERS:
count = db.update("person", values, selection, selectionArgs);
return count;
case USER:
long id = ContentUris.parseId(uri);
String where = "userid = "+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.update("user", values, where, selectionArgs);
return count;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
}
单元测试类(在另一个应用中):
Java代码
package com.yaku.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 对ContentProvider工程中的ContentProviderActivity进行单元测试
*/
public class ContentProviderActivityTest extends AndroidTestCase {
private static final String TAG = "ContentProvider";
//往内容提供者添加数据
public void testInsert() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri insertUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
ContentValues values = new ContentValues();
values.put("name", "道长");
values.put("age", 86);
Uri uri = contentResolver.insert(insertUri, values);
Log.i(TAG, uri.toString());
}
//更新内容提供者中的数据
public void testUpdate() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri updateUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user/1");
ContentValues values = new ContentValues();
values.put("name", "青眉道长");
contentResolver.update(updateUri, values, null, null);
}
//从内容提供者中删除数据
public void testDelete() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri deleteUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user/1");
contentResolver.delete(deleteUri, null, null);
}
//获取内容提供者中的数据
public void testFind() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri selectUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
Cursor cursor = contentResolver.query(selectUri, null, null, null, "userid desc");
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("userid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.i(TAG, "id="+ id + ",name="+ name+ ",age="+ age);
}
}
}
监听数据的变化:
Java代码
<span style="font-size: medium;">package com.yaku.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 监听数据变化
*/
public class OtherContentProviderTest extends AndroidTestCase {
private static final String TAG = "OtherContentProvider";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri insertUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
ContentResolver contentResolver = this.getContentResolver();
//对指定uri进行监听,如果该uri代表的数据发生变化,就会调用PersonObserver中的onChange()
contentResolver.registerContentObserver(insertUri, true, new PersonObserver(new Handler()));
}
private final class PersonObserver extends ContentObserver{
public PersonObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
ContentResolver contentResolver = getContentResolver();
Uri selectUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
Cursor cursor = contentResolver.query(selectUri, null, null, null, "userid desc");
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("userid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.i(TAG, "id="+ id + ",name="+ name+ ",age="+ age);
}
}
}
}
</span>
ContentProvider数据共享
1.首先在AndroidManifest.xml文件中添加对外暴露的数据共享接口Content
Xml代码
<provider android:name=".UserProvider" android:authorities="com.yaku.ContentProvider.userprovider"/>
ContentProvider采用了authorities(主机名/域名)对它进行唯一标识,authorities 就是他的域名
2.Url解析
content://com.yaku.ContentProvider.userprovider/user/2
【content://】 Android定义的内容提供都的Schema
【com.yaku.ContentProvider.userprovider】 主机名或者authorities
【user】 路径
【2】 ID
示例代码:
数据结构User.java:
Java代码
package com.yaku.pojo;
public class User {
private int id;
private String name;
private int age;
public User(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [age=" + age + ", id=" + id + ", name=" + name + "]";
}
}
数据库操作DBOpenHelper.java:
Java代码
package com.yaku.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DBOpenHelper extends SQLiteOpenHelper {
private static final String DBNAME = "yaku.db"; //数据库名称
private static final int DBVER = 1;//数据库版本
public DBOpenHelper(Context context) {
super(context, DBNAME, null, DBVER);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE user (userid integer primary key autoincrement, name varchar(20), age integer)";
db.execSQL(sql);//执行有更改的sql语句
}
@Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS user");
onCreate(db);
}
}
对外共享处理类ContentProviderUser.java:
Java代码
package com.yaku.ContentProvider;
import com.yaku.db.DBOpenHelper;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
public class ContentProviderUser extends ContentProvider {
private DBOpenHelper dbOpenHelper;
//常量UriMatcher.NO_MATCH表示不匹配任何路径的返回码
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int USERS = 1;
private static final int USER = 2;
static{
//如果match()方法匹配content://com.yaku.ContentProvider.userprovider/user路径,返回匹配码为1
MATCHER.addURI("com.yaku.ContentProvider.userprovider", "user", USERS);
//如果match()方法匹配content://com.yaku.ContentProvider.userprovider/user/123路径,返回匹配码为2
MATCHER.addURI("com.yaku.ContentProvider.userprovider", "user/#", USER);//#号为通配符
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case USERS:
count = db.delete("user", selection, selectionArgs);
return count;
case USER:
//ContentUris类用于获取Uri路径后面的ID部分
long id = ContentUris.parseId(uri);
String where = "userid = "+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.delete("user", where, selectionArgs);
return count;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
/**
* 该方法用于返回当前Url所代表数据的MIME类型。
* 如果操作的数据属于集合类型,那么MIME类型字符串应该以vnd.android.cursor.dir/开头
* 如果要操作的数据属于非集合类型数据,那么MIME类型字符串应该以vnd.android.cursor.item/开头
*/
@Override
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case USERS:
return "vnd.android.cursor.dir/user";
case USER:
return "vnd.android.cursor.item/user";
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case USERS:
long rowid = db.insert("user", "name", values);
Uri insertUri = ContentUris.withAppendedId(uri, rowid);//得到代表新增记录的Uri
this.getContext().getContentResolver().notifyChange(uri, null);
return insertUri;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public boolean onCreate() {
this.dbOpenHelper = new DBOpenHelper(this.getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case USERS:
return db.query("user", projection, selection, selectionArgs, null, null, sortOrder);
case USER:
long id = ContentUris.parseId(uri);
String where = "userid = "+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
return db.query("user", projection, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int count = 0;
switch (MATCHER.match(uri)) {
case USERS:
count = db.update("person", values, selection, selectionArgs);
return count;
case USER:
long id = ContentUris.parseId(uri);
String where = "userid = "+ id;
if(selection!=null && !"".equals(selection)){
where = selection + " and " + where;
}
count = db.update("user", values, where, selectionArgs);
return count;
default:
throw new IllegalArgumentException("Unkwon Uri:"+ uri.toString());
}
}
}
单元测试类(在另一个应用中):
Java代码
package com.yaku.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 对ContentProvider工程中的ContentProviderActivity进行单元测试
*/
public class ContentProviderActivityTest extends AndroidTestCase {
private static final String TAG = "ContentProvider";
//往内容提供者添加数据
public void testInsert() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri insertUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
ContentValues values = new ContentValues();
values.put("name", "道长");
values.put("age", 86);
Uri uri = contentResolver.insert(insertUri, values);
Log.i(TAG, uri.toString());
}
//更新内容提供者中的数据
public void testUpdate() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri updateUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user/1");
ContentValues values = new ContentValues();
values.put("name", "青眉道长");
contentResolver.update(updateUri, values, null, null);
}
//从内容提供者中删除数据
public void testDelete() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri deleteUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user/1");
contentResolver.delete(deleteUri, null, null);
}
//获取内容提供者中的数据
public void testFind() throws Throwable{
ContentResolver contentResolver = this.getContext().getContentResolver();
Uri selectUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
Cursor cursor = contentResolver.query(selectUri, null, null, null, "userid desc");
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("userid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.i(TAG, "id="+ id + ",name="+ name+ ",age="+ age);
}
}
}
监听数据的变化:
Java代码
<span style="font-size: medium;">package com.yaku.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 监听数据变化
*/
public class OtherContentProviderTest extends AndroidTestCase {
private static final String TAG = "OtherContentProvider";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri insertUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
ContentResolver contentResolver = this.getContentResolver();
//对指定uri进行监听,如果该uri代表的数据发生变化,就会调用PersonObserver中的onChange()
contentResolver.registerContentObserver(insertUri, true, new PersonObserver(new Handler()));
}
private final class PersonObserver extends ContentObserver{
public PersonObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
ContentResolver contentResolver = getContentResolver();
Uri selectUri = Uri.parse("content://com.yaku.ContentProvider.userprovider/user");
Cursor cursor = contentResolver.query(selectUri, null, null, null, "userid desc");
while(cursor.moveToNext()){
int id = cursor.getInt(cursor.getColumnIndex("userid"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.i(TAG, "id="+ id + ",name="+ name+ ",age="+ age);
}
}
}
}
</span>
相关文章推荐
- 使用android中ContentProvider组件对外共享以及监听ContentProvider中数据的变化
- Android疑惑之使用ContentProvider实现数据共享
- [转]Android共享数据ContentProvider的使用
- 5.4、Android中数据存储和访问-使用ContentProvider共享数据
- Android开发学习之使用ContentProvider实现数据共享
- Android学习19--使用ContentProvider实现数据共享
- Android 使用ContentProvider(内容提供者)共享数据
- Android深入探究笔记之十 -- 使用 ContentProvider 共享数据(二),访问与添加通讯录
- android学习:使用它ContentProvider获取其他程序共享的数据
- Android 使用ContentProvider对外共享数据与监听ContentProvider数据的变化
- Android应用使用ContentProvider共享数据
- 我的android 第21天 - 使用ContentProvider共享数据
- Android疑惑之使用ContentProvider实现数据共享
- android 使用ContentProvider共享数据
- Android:使用ContentProvider共享数据
- Android之使用Contentprovider对外共享数据和实现数据监听变化
- Android深入探究笔记之十 -- 使用 ContentProvider 共享数据(二),访问与添加通讯录
- Android 之使用ContentProvider(内容提供者)共享数据
- Android学习之 使用ContentProvider实现数据共享
- 无废话Android之listview入门,自定义的数据适配器、采用layoutInflater打气筒创建一个view对象、常用数据适配器ArrayAdapter、SimpleAdapter、使用ContentProvider(内容提供者)共享数据、短信的备份、插入一条记录到系统短信应用(3)