1.创建一个类继承SQLiteOpenHelper(若使用SQLite):
public class MySQLiteHelper extends SQLiteOpenHelper{ //重写构造方法 public MySQLiteHelper(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } //创建表 @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE person(personid INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),phone VARCHAR(20))" ); } //升级表(当Database的Version低于当前new里的Version,直接执行下面方法) @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { int v = newVersion - oldVersion; if(v > 0) { db.execSQL("ALTER TABLE person ADD salary3 VARCHAR(20)"); } }}
2.创建自定义ContentProvider:
public class MyContentProvider extends ContentProvider{ private MySQLiteHelper mDBHelper; public MyContentProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); int i = db.delete("person", selection,selectionArgs); return i; } @Override public String getType(Uri uri) { throw new UnsupportedOperationException("Not yet implemented"); } //创建Uri private static final Uri URI = Uri.parse("content://com.example.cpone"); @Override//插入 public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); long id = db.insert("person", null , values); return ContentUris.withAppendedId(URI, id); //相当于原先的Uri加上插入的行号 } @Override public boolean onCreate()//创建表 { mDBHelper = new MySQLiteHelper(getContext(), "person", null, 1); return false; } @Override//查询 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; } @Override//修改 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); int i = db.update("person", values, selection, selectionArgs); return i; }}
3.在另一个项目里使用自定义ContentProvider:
public class MyContentProvider extends ContentProvider{ private MySQLiteHelper mDBHelper; public MyContentProvider() { } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); int i = db.delete("person", selection,selectionArgs); return i; } @Override public String getType(Uri uri) { throw new UnsupportedOperationException("Not yet implemented"); } //创建Uri private static final Uri URI = Uri.parse("content://com.example.cpone"); @Override//插入 public Uri insert(Uri uri, ContentValues values) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); long id = db.insert("person", null , values); return ContentUris.withAppendedId(URI, id); //相当于原先的Uri加上插入的行号 } @Override public boolean onCreate()//创建表 { mDBHelper = new MySQLiteHelper(getContext(), "person", null, 1); return false; } @Override//查询 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); return cursor; } @Override//修改 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { SQLiteDatabase db = mDBHelper.getWritableDatabase(); int i = db.update("person", values, selection, selectionArgs); return i; }}
注:
1.ContentProvider仍在主线程上运行,但对他的调用是异步的,不使用消息队列。