contentprovider中UriMatcher matcher用法
1. 首先还是定义一个类继承contentprovider
package com.bwf.provider_01; import android.content.ContentProvider; import android.content.ContentUris; import android.content.ContentValues; import android.content.UriMatcher; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.net.Uri; import android.util.Log; public class DataProvider extends ContentProvider{ public static final Uri uri = Uri.parse("content://com.fanhy.provider/"); static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH); final static int DATAS = 0; final static int DATA = 1; DataHelper helper; SQLiteDatabase db; // 注册Uri static{ matcher.addURI("com.fanhy.provider", "per", DATAS); matcher.addURI("com.fanhy.provider", "per/#", DATA); } @Override public boolean onCreate() { myLog("onCreate"); helper = new DataHelper(getContext(), "Person.db", null, 1); db = helper.getReadableDatabase(); return true; } @Override public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) { myLog("query"); switch (matcher.match(uri)) { case DATAS: return db.query("per", null, null, null, null, null, null); case DATA: return db.query("per", null, selection, selectionArgs, null, null, null); default: throw new IllegalArgumentException("未知的uri:"+uri); } } @Override public String getType(Uri uri) { myLog("getType"); switch (matcher.match(uri)) { case DATAS: return "vnd.android.cursor.dir/com.fanhy.provider"; case DATA: return "vnd.android.cursor.item/com.fanhy.provider"; default: throw new IllegalArgumentException("未知的uri:"+uri); } } @Override public Uri insert(Uri uri, ContentValues values) { myLog("insert"); switch (matcher.match(uri)) { case DATAS: long rowId = db.insert("per", null, values); if(rowId > 0){ // 插入成功则返回新的把id添加进去形成新的uri Uri newUri = ContentUris.withAppendedId(uri, rowId); // 通知数据已更新 getContext().getContentResolver().notifyChange(newUri, null); return newUri; } return null; default: throw new IllegalArgumentException("未知的uri:"+uri); } } @Override public int delete(Uri uri, String selection, String[] selectionArgs) { myLog("delete"); switch (matcher.match(uri)) { case DATAS: return db.delete("per", null, null); case DATA: return db.delete("per", selection, selectionArgs); default: throw new IllegalArgumentException("未知的uri:"+uri); } } @Override public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { myLog("update"); switch (matcher.match(uri)) { case DATAS: return db.update("per", values, null, null); case DATA: return db.update("per", values, selection, selectionArgs); default: throw new IllegalArgumentException("未知的uri:"+uri); } } void myLog(String s){ Log.d("fanhy", s); } }
2. 创建一个类继承SQLiteOpenHealper
1 package com.bwf.provider_01; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteDatabase.CursorFactory; 6 import android.database.sqlite.SQLiteOpenHelper; 7 import android.util.Log; 8 9 public class DataHelper extends SQLiteOpenHelper{ 10 11 public DataHelper(Context context, String name, CursorFactory factory, 12 int version) { 13 super(context, name, factory, version); 14 } 15 16 @Override 17 public void onCreate(SQLiteDatabase db) { 18 String sql = "create table per(" + 19 "_id integer primary key autoincrement," + 20 "name varchar(8))"; 21 db.execSQL(sql); 22 Log.d("fanhy", "建立表格per"); 23 } 24 25 @Override 26 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 27 28 } 29 30 }
3.contentprovider的使用
package com.bwf.provider_01; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.ContentResolver; import android.content.ContentUris; import android.content.ContentValues; import android.database.Cursor; import android.util.Log; import android.view.Menu; import android.view.MenuItem; /** * 创建ContentProvider基本流程 * 1.定义一个类DataProvider继承自ContentProvider * 2.实现其onCreate、getType、insert、delete、update、query方法 * 3.在清单文件中注册ContentProvider * 4.将访问途径Uri提供出去 * * 使用ContentProvider基本流程 * 1.通过getContentResolver获取ContentResolver对象resolver * 2.通过Uri对ContentProvider中的数据进行增删改查操作 * */ public class MainActivity extends Activity { ContentResolver resolver; Uri uri = Uri.parse("content://com.fanhy.provider/per"); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); resolver = getContentResolver(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_add: ContentValues values = new ContentValues(); values.put("name", "张三"); resolver.insert(uri, values); values.put("name", "李思"); resolver.insert(uri, values); values.put("name", "张五"); resolver.insert(uri, values); break; case R.id.action_del: resolver.delete(uri, null, null); break; case R.id.action_update: ContentValues values1 = new ContentValues(); values1.put("name", "李四儿"); // 插入成功则返回新的把id添加进去形成新的uri Uri newUri = Uri.parse("content://com.fanhy.provider/per"); resolver.update(newUri, values1, "name = '李思'", null); break; case R.id.action_search: Cursor c = resolver.query(uri, null, null, null, null); while(c.moveToNext()){ String name = c.getString(c.getColumnIndex("name")); Log.d("fanhy", name); } break; default: break; } return super.onOptionsItemSelected(item); } }
4.当然还是的注册provider
<provider
android:name="com.bwf.provider_01.DataProvider"
android:authorities="com.fanhy.provider"
android:exported="true" >
</provider>
浙公网安备 33010602011771号