Android:自定义ContentProvider
1.创建一个类继承SQLiteOpenHelper(若使用SQLite): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 public class MySQLiteHelper extends SQLiteOpenHelper { //重写构造方法 public MySQLiteHelper(Contextcontext,Stringname,CursorFactoryfactory, int version) { super (context,name,factory,version); } //创建表 @Override public void onCreate(SQLiteDatabasedb) { db.execSQL( "CREATETABLEperson(personidINTEGERPRIMARYKEYAUTOINCREMENT,nameVARCHAR(20),phoneVARCHAR(20))" ); } //升级表(当Database的Version低于当前new里的Version,直接执行下面方法) @Override public void onUpgrade(SQLiteDatabasedb, int oldVersion, int newVersion) { int v=newVersion-oldVersion; if (v> 0 ) { db.execSQL( "ALTERTABLEpersonADDsalary3VARCHAR(20)" ); } } } 2.创建自定义ContentProvider: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class MyContentProvider extends ContentProvider { private MySQLiteHelpermDBHelper; public MyContentProvider() { } @Override public int delete(Uriuri,Stringselection,String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.delete( "person" ,selection,selectionArgs); return i; } @Override public StringgetType(Uriuri) { throw new UnsupportedOperationException( "Notyetimplemented" ); } //创建Uri private static final UriURI=Uri.parse( "content://com.example.cpone" ); @Override //插入 public Uriinsert(Uriuri,ContentValuesvalues) { SQLiteDatabasedb=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 Cursorquery(Uriuri,String[]projection,Stringselection, String[]selectionArgs,StringsortOrder) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); Cursorcursor=db.query( "person" ,projection,selection,selectionArgs, null , null ,sortOrder); return cursor; } @Override //修改 public int update(Uriuri,ContentValuesvalues,Stringselection, String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.update( "person" ,values,selection,selectionArgs); return i; } } 3.在另一个项目里使用自定义ContentProvider: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 public class MyContentProvider extends ContentProvider { private MySQLiteHelpermDBHelper; public MyContentProvider() { } @Override public int delete(Uriuri,Stringselection,String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.delete( "person" ,selection,selectionArgs); return i; } @Override public StringgetType(Uriuri) { throw new UnsupportedOperationException( "Notyetimplemented" ); } //创建Uri private static final UriURI=Uri.parse( "content://com.example.cpone" ); @Override //插入 public Uriinsert(Uriuri,ContentValuesvalues) { SQLiteDatabasedb=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 Cursorquery(Uriuri,String[]projection,Stringselection, String[]selectionArgs,StringsortOrder) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); Cursorcursor=db.query( "person" ,projection,selection,selectionArgs, null , null ,sortOrder); return cursor; } @Override //修改 public int update(Uriuri,ContentValuesvalues,Stringselection, String[]selectionArgs) { SQLiteDatabasedb=mDBHelper.getWritableDatabase(); int i=db.update( "person" ,values,selection,selectionArgs); return i; } } 注: 1.ContentProvider仍在主线程上运行,但对他的调用是异步的,不使用消息队列。 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1217552,如需转载请自行联系原作者