1、继承ContentProvider
2、在清单文件中进行配置
1 <provider android:name="com.example.mydbtest.PersonDBProvider"//全路径 2 android:authorities="com.example.mydbtest.personprovider"//表明相当于主机名的东西 3 ></provider>
3、内容提供者实行代码:
1 package com.example.mydbtest; 2 3 import com.example.mydbtest.dao.PersonDao; 4 5 import android.content.ContentProvider; 6 import android.content.ContentUris; 7 import android.content.ContentValues; 8 import android.content.UriMatcher; 9 import android.database.Cursor; 10 import android.database.sqlite.SQLiteDatabase; 11 import android.net.Uri; 12 13 public class PersonDBProvider extends ContentProvider { 14 15 //定义uri的匹配器,如果路径不满足,则返回-1 16 private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); 17 private PersonSQLiteOpenHelper helper; 18 //content:com.example.mydbtest.personprovider/insert 添加的操作 19 //content:com.example.mydbtest.personprovider/update 修改的操作 20 //content:com.example.mydbtest.personprovider/delete 删除的操作 21 //content:com.example.mydbtest.personprovider/query 查询的操作 22 23 private static final int INSERT =1; 24 private static final int DELETE =2; 25 private static final int UPDATE =3; 26 private static final int QUERY =4; 27 private static final int QUERYONE = 5; 28 static{ 29 //添加匹配规则,第一个参数相当于主机名;第二个参数是路径;第三个参数是匹配成功后的返回值;若匹配不成功,则返回-1 30 31 matcher.addURI("com.example.mydbtest.personprovider", "insert", INSERT); 32 matcher.addURI("com.example.mydbtest.personprovider", "delete", DELETE); 33 matcher.addURI("com.example.mydbtest.personprovider", "update", UPDATE); 34 matcher.addURI("com.example.mydbtest.personprovider", "query", QUERY); 35 36 matcher.addURI("com.example.mydbtest.personprovider", "query/#", QUERYONE);//#代表具体id,可以通过long id = ContentUris.parseId(uri);获取 37 } 38 @Override 39 //适合初始化 40 public boolean onCreate() { 41 // TODO Auto-generated method stub 42 helper = new PersonSQLiteOpenHelper(getContext()); 43 return false; 44 } 45 46 @Override 47 public Cursor query(Uri uri, String[] projection, String selection, 48 String[] selectionArgs, String sortOrder) { 49 // TODO Auto-generated method stub 50 if(matcher.match(uri) == QUERY){ 51 SQLiteDatabase db = helper.getReadableDatabase(); 52 Cursor cursor = db.query("person", projection, selection, selectionArgs, null, null, sortOrder); 53 return cursor;//注意这里请不要关闭数据库,否则cursor拿不到数据 54 }else{ 55 throw new IllegalArgumentException("路径不匹配,不能执行查询操作"); 56 } 57 58 } 59 60 @Override 61 public String getType(Uri uri) { 62 // TODO Auto-generated method stub 63 return null; 64 } 65 66 @Override 67 public Uri insert(Uri uri, ContentValues values) { 68 // TODO Auto-generated method stub 69 if(matcher.match(uri) == INSERT){ 70 SQLiteDatabase db = helper.getReadableDatabase(); 71 db.insert("person", null, values); 72 getContext().getContentResolver().notifyChange(PersonDao.messageuri, null); 73 }else{ 74 throw new IllegalArgumentException("路径不匹配,不能执行添加操作"); 75 } 76 return null; 77 } 78 79 @Override 80 public int delete(Uri uri, String selection, String[] selectionArgs) { 81 // TODO Auto-generated method stub 82 if(matcher.match(uri) == DELETE){ 83 SQLiteDatabase db = helper.getReadableDatabase(); 84 db.delete("person", selection, selectionArgs); 85 getContext().getContentResolver().notifyChange(PersonDao.messageuri, null); 86 }else{ 87 throw new IllegalArgumentException("路径不匹配,不能执行插入操作"); 88 } 89 return 0; 90 } 91 92 @Override 93 public int update(Uri uri, ContentValues values, String selection, 94 String[] selectionArgs) { 95 // TODO Auto-generated method stub 96 if(matcher.match(uri) == UPDATE){ 97 SQLiteDatabase db = helper.getReadableDatabase(); 98 db.update("person", values, selection, selectionArgs); 99 getContext().getContentResolver().notifyChange(PersonDao.messageuri, null); 100 }else{ 101 throw new IllegalArgumentException("路径不匹配,不能执行插入操作"); 102 } 103 return 0; 104 } 105 106 }
4、客户端测试代码:
1 package com.example.otherlistview; 2 3 import android.net.Uri; 4 import android.os.Bundle; 5 import android.app.Activity; 6 import android.content.ContentResolver; 7 import android.content.ContentValues; 8 import android.database.Cursor; 9 import android.view.Menu; 10 import android.view.View; 11 12 public class MainActivity extends Activity { 13 14 @Override 15 protected void onCreate(Bundle savedInstanceState) { 16 super.onCreate(savedInstanceState); 17 setContentView(R.layout.activity_main); 18 } 19 20 public void click(View view){ 21 //resolver相当于中间人 22 ContentResolver resolver = getContentResolver(); 23 Uri uri = Uri.parse("content://com.example.mydbtest.personprovider/query"); 24 Cursor cursor = resolver.query(uri, null, null, null, null); 25 while(cursor.moveToNext()){ 26 String name = cursor.getString(cursor.getColumnIndex("name")); 27 String id = cursor.getString(cursor.getColumnIndex("id")); 28 System.out.println("name=" + name + ", id=" + id); 29 } 30 31 cursor.close(); 32 } 33 34 public void delete(View view){ 35 ContentResolver resolver = getContentResolver(); 36 Uri uri = Uri.parse("content://com.example.mydbtest.personprovider/delete"); 37 resolver.delete(uri, "name=?", new String[]{"wangwu1"}); 38 } 39 40 public void update(View view){ 41 ContentResolver resolver = getContentResolver(); 42 Uri uri = Uri.parse("content://com.example.mydbtest.personprovider/update"); 43 ContentValues values = new ContentValues(); 44 values.put("number", "110"); 45 resolver.update(uri, values, "name=?", new String[]{"wangwu3"}); 46 } 47 48 public void insert(View view){ 49 ContentResolver resolver = getContentResolver(); 50 Uri uri = Uri.parse("content://com.example.mydbtest.personprovider/insert"); 51 ContentValues values = new ContentValues(); 52 values.put("name", "lisi"); 53 values.put("number", "110"); 54 values.put("account", 3000); 55 resolver.insert(uri, values); 56 } 57 58 }
原文:http://www.cnblogs.com/zhongyinghe/p/5295527.html