contentprovider


import android.content.ContentProvider;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.util.Log;

public class MyContentProvider extends ContentProvider {

	private static final String AUTHORITY = "com.example";
	private static final String TABLE_DATA = "data";
	public static final Uri APP_URI = Uri.parse("content://" + AUTHORITY + "/" + TABLE_DATA);
	private static final int MATCH_ALL_CODE = 100;
	private static UriMatcher uriMatcher;
	private ContentResolver contentResolver;
	private AppDBHelper dbHelper;

	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(AUTHORITY, TABLE_DATA, MATCH_ALL_CODE);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int id = 0;
		if (uriMatcher.match(uri) == MATCH_ALL_CODE) {
			SQLiteDatabase database = dbHelper.getWritableDatabase();
			id = database.delete(TABLE_DATA, selection, selectionArgs);
			contentResolver.notifyChange(uri, null);
		}
		return id;
	}

	@Override
	public String getType(Uri arg0) {
		throw new UnsupportedOperationException("Not yet implemented");
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		Uri u = null;
		if (uriMatcher.match(uri) == MATCH_ALL_CODE) {
			SQLiteDatabase database = dbHelper.getWritableDatabase();
			long d = database.replace(TABLE_DATA, null, values);
			u = ContentUris.withAppendedId(uri, d);
			contentResolver.notifyChange(u, null);
		}
		
		return u;
	}

	@Override
	public boolean onCreate() {
		Context context = getContext();
		contentResolver = context.getContentResolver();
		dbHelper = new AppDBHelper(context, "apps.db", null, 1);
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Cursor cursor = null;
		if (uriMatcher.match(uri) == MATCH_ALL_CODE) {
			SQLiteDatabase database = dbHelper.getReadableDatabase();
			cursor = database.query(TABLE_DATA, projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(contentResolver, uri);
		}

		return cursor;
	}

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		return 0;
	}

	private static class AppDBHelper extends SQLiteOpenHelper {

		public AppDBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
			super(context, name, factory, version);
		}

		@Override
		public void onCreate(SQLiteDatabase db) {
			String sql = "create table "+TABLE_DATA +"(key text not null unique primary key,value text not null);";
			Log.i("trans", sql);
			db.execSQL(sql);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			onCreate(db);
		}
	}

}
ContentResolver mContentResolver = getContentResolver();
HashMap<String, String> map = new HashMap<String, String>();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");

for (String key : map.keySet()) {
    ContentValues contentValues = new ContentValues();
    contentValues.put("key", key);
    contentValues.put("value", map.get(key));
    mContentResolver.insert(MyContentProvider.APP_URI, contentValues);
}
ContentResolver mContentResolver = getContentResolver();
Cursor cursor = mContentResolver.query(MyContentProvider.APP_URI, null, null, null, null);
String text = "";
if (cursor != null) {
    while (cursor.moveToNext()) {
        String key = cursor.getString(cursor.getColumnIndex("key"));
        String value = cursor.getString(cursor.getColumnIndex("value"));
        text += "key = " + key + ",value = " + value + "\n";
    }
    cursor.close();
    Log.i("trans", text);
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值