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);
}