Development of the four major Android components – ContentProvider.
ContentProvider is one of the four main components in Android, used for sharing and accessing data. To develop a ContentProvider, the following steps are required:
- Create a class that inherits from android.content.ContentProvider.
- Declare ContentProvider in the AndroidManifest.xml file.
- Implement several important methods in the ContentProvider class, such as onCreate, query, insert, update, and delete methods.
Here is a simple example of developing a ContentProvider.
- Create a class that inherits from ContentProvider.
public class MyContentProvider extends ContentProvider {
// 定义一个数据库的帮助类
private DatabaseHelper dbHelper;
@Override
public boolean onCreate() {
// 初始化数据库帮助类
dbHelper = new DatabaseHelper(getContext());
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
// 查询数据库数据,并返回一个Cursor对象
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("table_name", projection, selection, selectionArgs, null, null, sortOrder);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// 向数据库插入数据,并返回插入数据的Uri
SQLiteDatabase db = dbHelper.getWritableDatabase();
long id = db.insert("table_name", null, values);
return ContentUris.withAppendedId(uri, id);
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
// 更新数据库数据,并返回受影响的行数
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = db.update("table_name", values, selection, selectionArgs);
return count;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
// 删除数据库数据,并返回受影响的行数
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = db.delete("table_name", selection, selectionArgs);
return count;
}
@Override
public String getType(Uri uri) {
// 返回数据的MIME类型
return "vnd.android.cursor.dir/vnd.example.data";
}
}
- Declare ContentProvider in AndroidManifest.xml file.
<application>
...
<provider
android:name=".MyContentProvider"
android:authorities="com.example.mycontentprovider"
android:exported="true" />
...
</application>
- Using ContentProvider in other components.
// 查询数据
Uri uri = Uri.parse("content://com.example.mycontentprovider/table_name");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
do {
String data = cursor.getString(cursor.getColumnIndex("column_name"));
// 处理数据
} while (cursor.moveToNext());
cursor.close();
}
// 插入数据
Uri uri = Uri.parse("content://com.example.mycontentprovider/table_name");
ContentValues values = new ContentValues();
values.put("column_name", "value");
Uri newUri = getContentResolver().insert(uri, values);
// 更新数据
Uri uri = Uri.parse("content://com.example.mycontentprovider/table_name");
ContentValues values = new ContentValues();
values.put("column_name", "new value");
int count = getContentResolver().update(uri, values, "selection", new String[]{"selectionArgs"});
// 删除数据
Uri uri = Uri.parse("content://com.example.mycontentprovider/table_name");
int count = getContentResolver().delete(uri, "selection", new String[]{"selectionArgs"});
Here are the basic steps for using ContentProvider to share and access data. When developing a ContentProvider, it is important to pay attention to setting permissions and the related data operation logic.