ContentProvider使用示例
16lz
2021-01-26
ContentProvider使用示例
1、MyContentProvider
public class MyContentProvider extends ContentProvider { private static final String TAG = "MyContentProvider"; private Context context; private SQLiteDatabase sqLiteDatabase; public static final String AUTHORITY = "com.example.mycontentprovider.MyContentProvider"; public static final int PROVIDER_CODE = 0; private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { uriMatcher.addURI(AUTHORITY, MyDBHelper.TABLE_NAME, PROVIDER_CODE); } public MyContentProvider() { } /** * 通过uri匹配表名 * * @param uri * @return */ private String getTableName(Uri uri) { String tableName = null; switch (uriMatcher.match(uri)) { case PROVIDER_CODE: tableName = MyDBHelper.TABLE_NAME; break; } return tableName; } @Override public boolean onCreate() { init(); return false; } private void init() { context = getContext(); sqLiteDatabase = new MyDBHelper(context).getWritableDatabase(); } @Nullable @Override public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { String tableName = getTableName(uri); if (tableName == null) { Log.e(TAG, "query: 未匹配到uri"); throw new IllegalArgumentException("Unsupported URI:" + uri); } return sqLiteDatabase.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, null); } @Nullable @Override public String getType(@NonNull Uri uri) { return null; } @Nullable @Override public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) { String tableName = getTableName(uri); if (tableName == null) { Log.e(TAG, "insert: 未匹配到uri"); throw new IllegalArgumentException("Unsupported URI:" + uri); } sqLiteDatabase.insert(tableName, null, values); context.getContentResolver().notifyChange(uri, null); Log.d(TAG, "insert: 添加成功"); return uri; } @Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { String tableName = getTableName(uri); if (tableName == null) { Log.e(TAG, "delete: 未匹配到uri"); throw new IllegalArgumentException("Unsupported URI:" + uri); } int count = sqLiteDatabase.delete(tableName, selection, selectionArgs); if (count > 0) { context.getContentResolver().notifyChange(uri, null); } Log.d(TAG, "delete: 删除成功"); Log.d(TAG, "delete: count=" + count); return count; } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { String tableName = getTableName(uri); if (tableName == null) { throw new IllegalArgumentException("Unsupported URI:" + uri); } int row = sqLiteDatabase.update(tableName, values, selection, selectionArgs); if (row > 0) { context.getContentResolver().notifyChange(uri, null); } return row; }}
2、MyDBHelper
public class MyDBHelper extends SQLiteOpenHelper { /** * 库名 */ private static final String DBNAME = "provider.db"; /** * 表明 测试用 */ public static final String TABLE_NAME = "provider_data"; /** * 版本号 */ private static final int VERSION = 1; /** * 建表的sql语句 */ private static final String SQL = "create table " + TABLE_NAME + "(id integer primary key Autoincrement,name text)"; private Context context; public MyDBHelper(Context context) { super(context, DBNAME, null, VERSION); this.context = context; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(SQL); Toast.makeText(context, "建表成功", Toast.LENGTH_SHORT).show(); Log.d("MyContentProvider", "onCreate: 建表成功"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
3、在MainActivity中使用
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private static final String TAG = "MyContentProvider"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { Button bt_init = findViewById(R.id.bt_init); bt_init.setOnClickListener(this); Button bt_add = findViewById(R.id.bt_add); bt_add.setOnClickListener(this); Button bt_delete = findViewById(R.id.bt_delete); bt_delete.setOnClickListener(this); Button bt_update = findViewById(R.id.bt_update); bt_update.setOnClickListener(this); Button bt_query = findViewById(R.id.bt_query); bt_query.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.bt_init: init(); break; case R.id.bt_add: toAdd(); break; case R.id.bt_delete: toDelete(); break; case R.id.bt_update: toUpdate(); break; case R.id.bt_query: toQuery(); break; } } private void init() { getContentResolver().delete(nameUri, null, null); } private Uri nameUri = Uri.parse("content://com.example.mycontentprovider.MyContentProvider/provider_data"); private void toAdd() { ContentValues contentValues = new ContentValues(); contentValues.put("name", "水货"); getContentResolver().insert(nameUri, contentValues); } private void toDelete() { getContentResolver().delete(nameUri, "name=?", new String[]{"水货"}); } private void toUpdate() { ContentValues contentValues = new ContentValues(); contentValues.put("name", "太阳"); getContentResolver().update(nameUri, contentValues, "id=?", new String[]{"2"}); } private void toQuery() { Cursor nameCursor = getContentResolver().query(nameUri, new String[]{"id", "name"}, null, null, null); if (nameCursor != null) { while (nameCursor.moveToNext()) { Log.e(TAG, "id:" + nameCursor.getInt(nameCursor.getColumnIndex("id")) + " name:" + nameCursor.getString(nameCursor.getColumnIndex("name"))); } Log.d(TAG, "toQuery: 查询成功"); nameCursor.close(); } }}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
4、manifest
<?xml version="1.0" encoding="utf-8"?>
更多相关文章
- Android:Error:不允许有匹配 "[xX][mM][lL]" 的处理指令目标。
- Android视频采集+H264编码成功
- Android(安卓)定时重复启动弹出窗口。
- android 入门学习笔记 正则匹配 电话号码 email
- 基础总结篇之九:Intent应用详解
- Android(安卓)studio 添加依赖库的方法
- Android实现自动匹配关键字并且标红功能
- android基础总结篇之九:Intent应用详解
- Android请求数据格式与Java后台需求格式不匹配HttpMediaTypeNotS