顾名思义ContentProvider就是将自己的数据提供给别人,ContentResolver则是接受别人的数据
自定义一个类继承ContentProvider类
public class MyContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.example.contentprovider.MyContentProvider";//记得在清单文件中注册provide并命名,name必须为该类的完整名称(加包名),添加authority(域名)【为了让别人访问】,一般以包名命名
private static final int INSERT = 1; private static final int DELETE = 2; private static final int UPDATE = 3; private static final int QUERY = 4; MyHelp myhelp;//数据库的创建在这里就不说了 private static Context context; private ContentResolver mContent; static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); 匹配URI类 static{//在类加载的时候添加这些URI matcher.addURI(AUTHORITY, "insert", INSERT); matcher.addURI(AUTHORITY, "delete", DELETE); matcher.addURI(AUTHORITY, "update", UPDATE); matcher.addURI(AUTHORITY, "query", QUERY); }@Override
public int delete(Uri uri, String selection, String[] selectionArgs) { // TODO Auto-generated method stub if(matcher.match(uri)==DELETE) { SQLiteDatabase db=myhelp.getWritableDatabase(); db.delete("person", selection, selectionArgs); mContent.notifyChange(uri, null); }else { throw new IllegalArgumentException("uri not matched!"); } return 0; }@Override
public String getType(Uri uri) { // TODO Auto-generated method stub return null; }@Override
public Uri insert(Uri uri, ContentValues values) { // TODO Auto-generated method stub if(matcher.match(uri)==INSERT) { SQLiteDatabase db=myhelp.getWritableDatabase(); db.insert("person", null, values); mContent.notifyChange(uri, null); }else { throw new IllegalArgumentException("uri not matched!"); } return null; }@Override
public boolean onCreate() { // TODO Auto-generated method stub context = this.getContext(); myhelp = new MyHelp(context); mContent = context.getContentResolver(); return false; }@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { // TODO Auto-generated method stub if(matcher.match(uri)==QUERY) { SQLiteDatabase db=myhelp.getWritableDatabase(); db.query("person", projection, selection, selectionArgs, null, null, null); mContent.notifyChange(uri, null); }else { throw new IllegalArgumentException("uri not matched!"); } return null; }@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { // TODO Auto-generated method stub if(matcher.match(uri)==UPDATE) { SQLiteDatabase db=myhelp.getWritableDatabase(); db.update("person", values, selection, selectionArgs); mContent.notifyChange(uri, null); }else { throw new IllegalArgumentException("uri not matched!"); } return 0; }}
在另一个测试应用中,这两个不是一个应用,用于访问上面的应用,要用到ContentResolver
public class MainActivity extends Activity {
private static final String AUTHORITY = "com.example.contentprovider.MyContentProvider"; ContentResolver mContent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContent = getContentResolver(); mContent.registerContentObserver(Uri.parse("content://"+AUTHORITY), true,new MyObserver(new Handler()));//注册监视,观察数据是否发生改变(可以不加该方法) } class MyObserver extends ContentObserver {public MyObserver(Handler handler) {
super(handler); // TODO Auto-generated constructor stub }@Override
public void onChange(boolean selfChange) { // TODO Auto-generated method stub super.onChange(selfChange); Toast.makeText(MainActivity.this, "is changed...", 0).show(); } } public void insert(View v)//按钮的点击事件,在布局文件中已经定义 { Uri uri = Uri.parse("content://" + AUTHORITY + "/insert");ContentValues cv = new ContentValues();
cv.put("id", 4);
cv.put("name", "tom"); mContent.insert(uri, cv); } public void delete(View v) { Uri uri = Uri.parse("content://" + AUTHORITY + "/delete"); mContent.delete(uri, "id=?", new String[]{String.valueOf(4)}); } public void update(View v) { Uri uri = Uri.parse("content://" + AUTHORITY + "/update"); ContentValues cv = new ContentValues();cv.put("name", "lily");
mContent.update(uri, cv, "id=?",
new String[] { String.valueOf(2) }); } public void query(View v) { Uri uri = Uri.parse("content://" + AUTHORITY + "/query"); mContent.query(uri, null, null, null, null); }