Android中ContentProvider和ContentResolver详解
package com.backgroundservice;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.NET.Uri;
import android.util.Log;
/**
* TODO
*
* @author zengpeng
* @version 1.0 Create At : 2010-3-18 下午02:52:40
*/
public class TestContentProvider extends ContentProvider {
private SQLiteDatabase mDb;
private DatabaseHelper mDbHelper = null;
private static final String DATABASE_NAME = "rssitems.db";
private static final String DATABASE_TABLE_NAME = "rssItems";
private static final int DB_VERSION = 1;
private static final int ALL_MESSAGES = 1;
private static final int SPECIFIC_MESSAGE = 2;
// Set up our URL matchers to help us determine what an
// incoming URI parameter is.
private static final UriMatcher URI_MATCHER;
static {
URI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
URI_MATCHER.addURI("test", "item", ALL_MESSAGES);
URI_MATCHER.addURI("test", "item/#", SPECIFIC_MESSAGE);
}
// Here's the public URI used to query for RSS items.
public static final Uri CONTENT_URI = Uri
.parse("content://test/item");
// Here are our column name constants, used to query for field values.
public static final String ID = "_id";
public static final String NAME = "NAME";
public static final String VALUE = "VALUE";
public static final String DEFAULT_SORT_ORDER = ID + " DESC";
private static class DatabaseHelper extends AbstractDatabaseHelper {
@Override
protected String[] createDBTables() {
// TODO Auto-generated method stub
String sql = "CREATE TABLE " + DATABASE_TABLE_NAME + "(" + ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + NAME + " TEXT,"
+ VALUE + " TEXT);";
return new String[] { sql };
}
@Override
protected String[] dropDBTables() {
// TODO Auto-generated method stub
return null;
}
@Override
protected String getDatabaseName() {
// TODO Auto-generated method stub
return DATABASE_NAME;
}
@Override
protected int getDatabaseVersion() {
// TODO Auto-generated method stub
return DB_VERSION;
}
@Override
protected String getTag() {
// TODO Auto-generated method stub
return TestContentProvider.class.getSimpleName(); }
}
/**
*
*/
public TestContentProvider() {
// TODO Auto-generated constructor stub
}
/*
* (non-Javadoc)
*
* @see android.content.ContentProvider#delete(android.Net.Uri,
* Java.lang.String, java.lang.String[])
*/
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// NOTE Argument checking code omitted. Check your parameters!
int rowCount = mDb.delete(DATABASE_TABLE_NAME, selection, selectionArgs);
// Notify any listeners and return the deleted row count.
getContext().getContentResolver().notifyChange(uri, null);
return rowCount;
}
/*
* (non-Javadoc)
*
* @see android.content.ContentProvider#getType(android.net.Uri)
*/
@Override
public String getType(Uri uri) {
switch (URI_MATCHER.match(uri)) {
case ALL_MESSAGES:
return "vnd.android.cursor.dir/rssitem"; // List of items.
case SPECIFIC_MESSAGE:
return "vnd.android.cursor.item/rssitem"; // Specific item.
default:
return null;
}
}
/*
* (non-Javadoc)
*
* @see android.content.ContentProvider#insert(android.net.Uri,
* android.content.ContentValues)
*/
@Override
public Uri insert(Uri uri, ContentValues values) {
// NOTE Argument checking code omitted. Check your parameters! Check that
// your row addition request succeeded!
long rowId = -1;
rowId = mDb.insert(DATABASE_TABLE_NAME, NAME, values);
Uri newUri = Uri.withAppendedPath(CONTENT_URI, ""+rowId);
Log.i("TestContentProvider", "saved a record " + rowId + " " + newUri);
// Notify any listeners and return the URI of the new row.
getContext().getContentResolver().notifyChange(CONTENT_URI, null);
return newUri;
}
/*
* (non-Javadoc)
*
* @see android.content.ContentProvider#onCreate()
*/
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
try
{
mDbHelper = new DatabaseHelper();
mDbHelper.open(getContext());
mDb = mDbHelper.getMDb();
}catch(Exception e){
e.printStackTrace();
}
return true;
}
/*
* (non-Javadoc)
*
* @see android.content.ContentProvider#query(android.net.Uri,
* java.lang.String[], java.lang.String, java.lang.String[],
* java.lang.String)
*/
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// We won't bother checking the validity of params here, but you should!
// SQLiteQueryBuilder is the helper class that creates the
// proper SQL syntax for us.
SQLiteQueryBuilder qBuilder = new SQLiteQueryBuilder();
// Set the table we're querying.
qBuilder.setTables(DATABASE_TABLE_NAME);
// If the query ends in a specific record number, we're
// being asked for a specific record, so set the
// WHERE clause in our query.
if((URI_MATCHER.match(uri)) == SPECIFIC_MESSAGE){
qBuilder.appendWhere("_id=" + uri.getLastPathSegment());
Log.i("TestContentProvider", "_id=" + uri.getLastPathSegment());
}
// Make the query.
Cursor c = qBuilder.query(mDb,
projection,
selection,
selectionArgs,
null,
null,
sortOrder);
Log.i("TestContentProvider", "get records");
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
/*
* (non-Javadoc)
*
* @see android.content.ContentProvider#update(android.net.Uri,
* android.content.ContentValues, java.lang.String, java.lang.String[])
*/
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
// NOTE Argument checking code omitted. Check your parameters!
int updateCount = mDb.update(DATABASE_TABLE_NAME, values, selection, selectionArgs);
// Notify any listeners and return the updated row count.
getContext().getContentResolver().notifyChange(uri, null);
return updateCount;
}
}
- .NET Core系列之MemoryCache 初识
- 007手机一键Root(安机网一键Root) v3.0 官方最新版 一键ROOT您的Android手机
- 12306密码被盗了怎么办?12306密码外泄解决方法
- 12个字的qq网名
- 150M迷你型无线路由器怎么设置?
- 192.168.1.1打不开怎么办?路由器192.168.1.1打不开的原因以及解决办法
- 2011年电子报合订本 电子报 编辑部 中文 PDF版 [84M]
- 2015年1月15日小米新旗舰发布会现场图文直播
- 2016.3.1vivo Xplay5新品发布会现场视频直播 优酷直播
- 2016华为P9发布会视频直播地址 4月15日华为P9国行发布会直播
相关文章
- Android自定义ViewPager实现个性化的图片切换效果
- 5sing电台 5sing中国原创音乐基地 for Android v5.8.4 安卓版
- Android使用socket进行二进制流数据传输
- android studio 清单配置文件androidmainfest.xml详细解读
- Android多功能视频播放器GSYVideoPlayer开发流程
- Android自定义控件EditText使用详解
- Android 缩放动画 ScaleAnimation的使用小结
- Android开发环境搭建
- Android中PackageManager使用详解
- Android Handler消息传递机制