package com.hoperun;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.UriMatcher;
import android.database.ContentObserver;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
public class MyProvider extends ContentProvider {
private static final String TAG = "provider";
private Context mContext;
private DbHelper helper;
private SQLiteDatabase database;
private static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(ConstantsUtil.AUTHORITY, ConstantsUtil.PATH, ConstantsUtil.CODE_ALL_DATA);
uriMatcher.addURI(ConstantsUtil.AUTHORITY, ConstantsUtil.PATH+"/#", ConstantsUtil.CODE_SPECIFIC_DATA);
}
@Override
public boolean onCreate() {
//外界调用ContentResolver时调用
mContext = this.getContext();
helper = new DbHelper(mContext);
return true;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)){
case ConstantsUtil.CODE_ALL_DATA:
return ConstantsUtil.CONTENT_ALL_TYPE;
case ConstantsUtil.CODE_SPECIFIC_DATA:
return ConstantsUtil.CONTENT_SPECIFIC_TYPE;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
}
@Override
public Uri insert(Uri uri, ContentValues values) {
database = helper.getWritableDatabase();
long rowId = database.insert(ConstantsUtil.TABLE,"",values);
Uri noteUri = ContentUris.withAppendedId(ConstantsUtil.CONTENT_URI, rowId);
getContext().getContentResolver().notifyChange(noteUri, null);
return noteUri;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
database = helper.getWritableDatabase();
int count = 0;
switch (uriMatcher.match(uri)) {
case ConstantsUtil.CODE_ALL_DATA:
count = database.delete(ConstantsUtil.TABLE, selection, selectionArgs);
break;
case ConstantsUtil.CODE_SPECIFIC_DATA:
//uri.getPathSegments()得到一个集合,内容分别为 uri的path和_id 即 [provider , _id]
String id = uri.getPathSegments().get(1);
count = database.delete(ConstantsUtil.TABLE, ConstantsUtil.TID+"="+id+(!TextUtils.isEmpty(selection)?" AND("+selection+')':""),selectionArgs);
break;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
//getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int count = 0;
database = helper.getWritableDatabase();
count = database.update(ConstantsUtil.TABLE, values, selection, selectionArgs);
/*getContext().getContentResolver().registerContentObserver(uri, true, new ContentObserver(new Handler()){
@Override
public void onChange(boolean selfChange) {
super.onChange(selfChange);
}
});*/
return count;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
database = helper.getReadableDatabase();
Cursor c = null;
switch (uriMatcher.match(uri)) {
case ConstantsUtil.CODE_ALL_DATA:
c = database.query(ConstantsUtil.TABLE, projection, selection, selectionArgs, null, null, null);
break;
case ConstantsUtil.CODE_SPECIFIC_DATA:
//uri.getPathSegments()得到一个集合,内容分别为 uri的path和_id 即 [provider , _id]
String id = uri.getPathSegments().get(1);
c = database.query(ConstantsUtil.TABLE, projection, ConstantsUtil.TID+"="+id+(!TextUtils.isEmpty(selection)?" AND("+selection+')':""),selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown URI"+uri);
}
return c;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android ContentProvider 使用详解源码
共59个文件
class:19个
png:12个
java:8个
5星 · 超过95%的资源 需积分: 3 31 下载量 34 浏览量
2011-11-07
14:37:22
上传
评论
收藏 126KB ZIP 举报
温馨提示
ContentProvider 与 ContentResovler 进行应用间数据共享
资源推荐
资源详情
资源评论
收起资源包目录
ContentProviderDemo.zip (59个子文件)
ContentProviderDemo
bin
classes.dex 8KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
classes
com
hoperun
ContentProviderDemoActivity.class 2KB
R$layout.class 373B
R$id.class 359B
R$drawable.class 386B
MyProvider.class 5KB
R$attr.class 319B
DbHelper.class 2KB
R.class 482B
R$string.class 406B
ConstantsUtil.class 1KB
resources.ap_ 10KB
ContentProviderDemo.apk 16KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 204B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 396B
assets
gen
com
hoperun
R.java 731B
proguard.cfg 1KB
src
com
hoperun
DbHelper.java 1KB
MyProvider.java 4KB
ContentProviderDemoActivity.java 1KB
ConstantsUtil.java 1KB
.project 855B
.classpath 364B
project.properties 360B
AndroidManifest.xml 935B
ContentResolverDemo
bin
classes.dex 6KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
drawable-mdpi
ic_launcher.png 2KB
ContentResolverDemo.apk 15KB
classes
com
zj
content
R$layout.class 382B
R$id.class 451B
R$drawable.class 395B
ContentResolverDemoActivity$1.class 3KB
R$attr.class 328B
R.class 503B
ContentResolverDemoActivity.class 3KB
R$string.class 415B
ConstantsUtil.class 1KB
resources.ap_ 11KB
res
drawable-ldpi
ic_launcher.png 2KB
drawable-hdpi
ic_launcher.png 4KB
values
strings.xml 204B
drawable-mdpi
ic_launcher.png 3KB
layout
main.xml 799B
assets
gen
com
zj
content
R.java 883B
proguard.cfg 1KB
src
com
zj
content
ContentResolverDemoActivity.java 3KB
ConstantsUtil.java 1KB
.project 855B
.classpath 364B
project.properties 360B
AndroidManifest.xml 743B
共 59 条
- 1
资源评论
- u0107466842013-06-19例子简明易懂
- 0点起航2014-07-16真的很不错哦
- q25544962013-09-06例子挺简单。我学习啦。
- 我方2013-04-16例子挺详细。我学习啦。
Cappuccino
- 粉丝: 49
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功