android Contextprovider 对外共享数据
来源:互联网 发布:上海市物业大楼数据 编辑:程序博客网 时间:2024/05/22 01:36
Contextprovider 跨app数据共享(以sqlite db为例)
sqlite db 所在app程序:
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 InfoProvider extends ContentProvider {
private DBHelper dbOpenHelper;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
private static final int INFOS = 1;
private static final int INFO = 2;
static{
MATCHER.addURI("com.example.providers.infoprovider", "info", INFOS);
MATCHER.addURI("com.example.providers.infoprovider", "info/#", INFO);
}
@Override
public boolean onCreate() {
dbOpenHelper = new DBHelper(this.getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbOpenHelper.getReadableDatabase();
switch (MATCHER.match(uri)) {
case INFOS:
return db.query("info", projection, selection, selectionArgs, null, null, sortOrder);
case INFO:
long rowid = ContentUris.parseId(uri);
String where = "id="+ rowid;
if(selection!=null && !"".equals(selection.trim())){
where += " and "+ selection;
}
return db.query("info", projection, where, selectionArgs, null, null, sortOrder);
default:
throw new IllegalArgumentException("this is Unknown Uri:"+ uri);
}
}
@Override
public String getType(Uri uri) {
switch (MATCHER.match(uri)) {
case INFOS:
return "vnd.android.cursor.dir/person";
case INFO:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("this is Unknown Uri:"+ uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
switch (MATCHER.match(uri)) {
case INFOS:
long rowid = db.insert("info", "name", values);//主键值
// content://cn.itcast.provides.personprovider/person/10
Uri insertUri = ContentUris.withAppendedId(uri, rowid);
this.getContext().getContentResolver().notifyChange(uri, null);//发出数据变化通知
return insertUri;
default:
throw new IllegalArgumentException("this is Unknown Uri:"+ uri);
}
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case INFOS:
num = db.delete("info", selection, selectionArgs);
break;
case INFO:
long rowid = ContentUris.parseId(uri);
String where = "id="+ rowid;
if(selection!=null && !"".equals(selection.trim())){
where += " and "+ selection;
}
num = db.delete("info", where, selectionArgs);
break;
default:
throw new IllegalArgumentException("this is Unknown Uri:"+ uri);
}
return num;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbOpenHelper.getWritableDatabase();
int num = 0;
switch (MATCHER.match(uri)) {
case INFOS:
num = db.update("info", values, selection, selectionArgs);
break;
case INFO:
long rowid = ContentUris.parseId(uri);
String where = "id="+ rowid;
if(selection!=null && !"".equals(selection.trim())){
where += " and "+ selection;
}
num = db.update("info", values, where, selectionArgs);
break;
default:
throw new IllegalArgumentException("this is Unknown Uri:"+ uri);
}return num;
}
}
2.在另外一个 App中 调用 InfoProvider 操作数据
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 InfoProviderTest extends AndroidTestCase {
private static final String TAG = "InfoProviderTest";
public void testInsert() throws Exception{
Uri uri = Uri.parse("content://com.example.providers.infoprovider/info");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "acct");
values.put("age", "10");
values.put("sex", "男");
resolver.insert(uri, values);
}
public void testDelete() throws Exception{
Uri uri = Uri.parse("content://com.example.providers.infoprovider/info/60");
ContentResolver resolver = this.getContext().getContentResolver();
resolver.delete(uri, null, null);
}
public void testUpdate() throws Exception{
Uri uri = Uri.parse("content://com.example.providers.infoprovider/info/62");
ContentResolver resolver = this.getContext().getContentResolver();
ContentValues values = new ContentValues();
values.put("name", "hehe");
resolver.update(uri, values, null, null);
}
public void testQuery() throws Exception{
Uri uri = Uri.parse("content://com.example.providers.infoprovider/info");
ContentResolver resolver = this.getContext().getContentResolver();
Cursor cursor = resolver.query(uri, null, null, null, "id asc");
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.i(TAG, name);
}
cursor.close();
}
}
- android Contextprovider 对外共享数据
- Android 对外共享数据
- android 编写content_provider对外共享数据
- 十、android的ContentProvider对外共享数据
- android数据对外共享的方式----contentProvider
- android 编写content_provider对外共享数据
- android ContextProvider
- ContentProvider 对外共享数据
- Android开发入门之采用ContentProvider对外共享数据
- 使用ContentProvider对外共享数据
- Android之使用Contentprovider对外共享数据和实现数据监听变化
- Android 使用ContentProvider对外共享数据与监听ContentProvider数据的变化
- 实例教程九:采用ContentProvider对外共享数据
- Android心得5.1--第一个内容操作者实例介绍-ContentProvide对外共享数据
- 使用android中ContentProvider组件对外共享以及监听ContentProvider中数据的变化
- Android心得--第一个内容操作者实例介绍-ContentProvide对外共享数据
- Android的四大组件之ContentProvider(对外程序共享数据)
- 采用ContentProvider对外共享数据,ContentResolver获取数据
- .Net魔法堂:提取注释生成API文档
- HashMap、Hashtable、HashSet的比较
- TextBox滚动条
- 语义分析若干知识
- 去掉和隐藏应用
- android Contextprovider 对外共享数据
- unzip 命令 乱码
- UI - 简单封装视图
- android使用java代码创建控件、布局
- 理解OAuth 2.0
- DLNA DMR实现
- fopen: failed to open stream: Permission de
- 第三方推送服务:个推服务推送流程
- 学习html5的video标签