小编给大家分享一下Android中ContentProvider基础应用的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
一、适用场景
ContentProvider为存储和读取数据提供了统一的接口
使用ContentProvider,应用程序可以实现数据共享
android内置的许多数据都是使用ContentProvider形式,供开发者调用的(如视频,音频,图片,通讯录等)
二、概念介绍
1、ContentProvider简介
当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据。虽然使用其他方法也可以对外共享数据,但数据访问方式会因数据存储的方式而不同,如:采用文件方式对外共享数据,需要进行文件操作读写数据;采用sharedpreferences共享数据,需要使用sharedpreferences API读写数据。而使用ContentProvider共享数据的好处是统一了数据访问方式。
2、Uri类简介
Uri uri = Uri.parse("content://com.changcheng.provider.contactprovider/contact")
在Content Provider中使用的查询字符串有别于标准的SQL查询。很多诸如select, add, delete, modify等操作我们都使用一种特殊的URI来进行,这种URI由3个部分组成, “content://”, 代表数据的路径,和一个可选的标识数据的ID。以下是一些示例URI:
content://media/internal/images 这个URI将返回设备上存储的所有图片
content://contacts/people/ 这个URI将返回设备上的所有联系人信息
content://contacts/people/45 这个URI返回单个结果(联系人信息中ID为45的联系人记录)
尽管这种查询字符串格式很常见,但是它看起来还是有点令人迷惑。为此,Android提供一系列的帮助类(在android.provider包下),里面包含了很多以类变量形式给出的查询字符串,这种方式更容易让我们理解一点,因此,如上面content://contacts/people/45这个URI就可以写成如下形式:Uri person = ContentUris.withAppendedId(People.CONTENT_URI, 45)。
三、使用步骤
1、首先创建一个继承自ContentProvider的类,并实现其6个方法:
此6个方法只有onCreate方法运行在UI线程中,这里在onCreate方法中进行一些数据的初始化工作,初始了SQLite数据库【这里拿封装数据库操作举例,也可以是其他】,创建了两张表book、user 并分别默认插入了两本书,和两个管理员。
package com.hongri.androidipc.contentprovider; public class MyContentProvider extends ContentProvider { private static final String TAG = MyContentProvider.class.getSimpleName() + " "; public static final String AUTHORITIES = "com.hongri.androidipc.contentprovider.provider"; public static final Uri BOOK_CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/book"); public static final Uri USER_CONTENT_URI = Uri.parse("content://" + AUTHORITIES + "/user"); public static final int BOOK_URI_CODE = 0; public static final int USER_URI_CODE = 1; private DbOpenHelper mDbHelper; private SQLiteDatabase mDB; private Context mContext; private static final UriMatcher mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH); static { mUriMatcher.addURI(AUTHORITIES, "book", BOOK_URI_CODE); mUriMatcher.addURI(AUTHORITIES, "user", USER_URI_CODE); } @Override public boolean onCreate() { String currentThreadName = Thread.currentThread().getName(); Logger.d(TAG + "onCreate--" + "currentThreadName:" + currentThreadName); mContext = getContext(); mDbHelper = new DbOpenHelper(getContext(), "", null, 1); mDB = mDbHelper.getWritableDatabase(); mDB.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME); mDB.execSQL("delete from " + DbOpenHelper.USER_TABLE_NAME); mDB.execSQL("insert into book values('1','Android',234);"); mDB.execSQL("insert into book values('2','Java',348);"); mDB.execSQL("insert into user values('1','hongri',1);"); mDB.execSQL("insert into user values('2','huyin',1);"); return true; } private String getTableName(Uri uri) { switch (mUriMatcher.match(uri)) { case BOOK_URI_CODE: return DbOpenHelper.BOOK_TABLE_NAME; case USER_URI_CODE: return DbOpenHelper.USER_TABLE_NAME; default: break; } return ""; } @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); mDB.insert(tableName, null, values); mContext.getContentResolver().notifyChange(uri, null); return uri; } @Override public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) { String tableName = getTableName(uri); int count = mDB.delete(tableName, selection, selectionArgs); if (count > 0) { mContext.getContentResolver().notifyChange(uri, null); } return count; } @Nullable @Override public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) { String currentThreadName = Thread.currentThread().getName(); Logger.d(TAG + "query: currentThreadName:" + currentThreadName + " uri:" + uri); String tableName = getTableName(uri); return mDB.query(tableName, projection, selection, selectionArgs, null, null, sortOrder, null); } @Override public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) { String tableName = getTableName(uri); if (tableName == null) { return 0; } int row = mDB.update(tableName, values, selection, selectionArgs); if (row > 0) { mContext.getContentResolver().notifyChange(uri, null); } return row; }}
上面封装的 DbOpenHelper 代码如下:
package com.hongri.androidipc.db; public class DbOpenHelper extends SQLiteOpenHelper { public static final String BOOK_TABLE_NAME = "book"; public static final String USER_TABLE_NAME = "user"; public static final String DB_NAME = "library"; public static final int DB_VERSION = 1; private String BOOK_SQL; private String USER_SQL; public DbOpenHelper(Context context, String name, CursorFactory factory, int version) { super(context, DB_NAME, factory, DB_VERSION); BOOK_SQL = "CREATE TABLE IF NOT EXISTS " + BOOK_TABLE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT," + "page INT)"; USER_SQL = "CREATE TABLE IF NOT EXISTS " + USER_TABLE_NAME + "(_id INTEGER PRIMARY KEY," + "name TEXT," + "sex INT)"; } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(BOOK_SQL); db.execSQL(USER_SQL); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }}
2、在Manifest文件中注册这个ContentProvider:
<provider android:name="com.hongri.androidipc.contentprovider.MyContentProvider" android:authorities="com.hongri.androidipc.contentprovider.provider" android:permission="com.hongri.androidipc.PROVIDER" android:process=":provider" />
3、在外部应用中访问它:
<activity android:name=".ContentProviderActivity" android:process=":remoteProcess"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity>
这里单独开启了一个进程 remoteProcess 用来模拟第三方App。
该Activity页面如下,点击相关按钮可以进行基础的增、删、查、改操作,可通过log查看调用结果。
这里注意如果希望监听数据更新,那么需要注册内容观察者 ContentObserver,增、删、改动的时候,会有回调通知,前提是在自定义的ContentProvider类方法中,调用getContentResolver().notifyChange(uri, null);方法即可。
package com.hongri.androidipc; public class ContentProviderActivity extends AppCompatActivity implements View.OnClickListener { //book uri private final Uri bookUri = MyContentProvider.BOOK_CONTENT_URI; //user uri private final Uri userUri = MyContentProvider.USER_CONTENT_URI; private Button btnInsert, btnQuery, btnQueryByUser, btnModify, btnDelete; private Handler mHandler = new Handler(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_content_provider); btnInsert = (Button) findViewById(R.id.btnInsert); btnQuery = (Button) findViewById(R.id.btnQuery); btnQueryByUser = (Button) findViewById(R.id.btnQueryByUser); btnModify = (Button) findViewById(R.id.btnModify); btnDelete = (Button) findViewById(R.id.btnDelete); btnInsert.setOnClickListener(this); btnQuery.setOnClickListener(this); btnQueryByUser.setOnClickListener(this); btnModify.setOnClickListener(this); btnDelete.setOnClickListener(this); getContentResolver().registerContentObserver(bookUri, true, mContentObserver); getContentResolver().registerContentObserver(userUri, true, mContentObserver); } private ContentObserver mContentObserver = new ContentObserver(mHandler) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); //这里selfChange都是返回false的,不用理会这个参数 Logger.d("onChange调用 --- 数据有更新"); } }; private void doInsert() { ContentValues values = new ContentValues(); values.put("name", "Android框架"); values.put("page", "1213"); Uri uri = getContentResolver().insert(bookUri, values); Logger.d("插入成功 ---> uri:" + uri.toString()); } private void doQuery() { Cursor bookCursor = getContentResolver().query(bookUri, new String[]{"_id", "name", "page"}, null, null, null); while (bookCursor.moveToNext()) { Book book = new Book(); book.set_id(bookCursor.getInt(0)); book.setName(bookCursor.getString(1)); book.setPage(bookCursor.getInt(2)); Logger.d("book:" + book.toString()); } bookCursor.close(); } private void doQueryUser() { Cursor userCursor = getContentResolver().query(userUri, new String[]{"_id", "name", "sex"}, null, null, null); while (userCursor.moveToNext()) { User user = new User(); user.set_id(userCursor.getInt(0)); user.setName(userCursor.getString(1)); user.setSex(userCursor.getInt(2)); Logger.d("user:" + user.toString()); } userCursor.close(); } private void doUpdate() { ContentValues updateValues = new ContentValues(); updateValues.put("name", "Android底层开发"); updateValues.put("page", 3345); int row = getContentResolver().update(bookUri, updateValues, "name=?", new String[]{"Android框架"}); if (row > 0) { Logger.d("book:已修改"); } } private void doDelete() { int count = getContentResolver().delete(bookUri, "name=?", new String[]{"Java"}); if (count > 0) { Logger.d("book:已进行删除操作"); } } @SuppressLint("NonConstantResourceId") @Override public void onClick(View v) { int id = v.getId(); switch (id) { case R.id.btnInsert: doInsert(); break; case R.id.btnQuery: doQuery(); break; case R.id.btnQueryByUser: doQueryUser(); break; case R.id.btnModify: doUpdate(); doQuery(); break; case R.id.btnDelete: doDelete(); doQuery(); break; default: break; } } @Override protected void onDestroy() { super.onDestroy(); getContentResolver().unregisterContentObserver(mContentObserver); }}
以上是“Android中ContentProvider基础应用的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注编程网行业资讯频道!