ContentProvider、ContentResolver的主要代码
时间: 2024-06-12 18:07:50 浏览: 91
ContentProvider:
```java
public class MyContentProvider extends ContentProvider {
// 数据库名称
private static final String DB_NAME = "my_db";
// 数据库版本号
private static final int DB_VERSION = 1;
// 表名
private static final String TABLE_NAME = "my_table";
// ContentProvider的Uri
public static final Uri CONTENT_URI = Uri.parse("content://com.example.mycontentprovider");
// ContentProvider支持的MIME类型
private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.com.example.mycontentprovider";
private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.com.example.mycontentprovider";
// 数据库操作对象
private SQLiteDatabase db;
@Override
public boolean onCreate() {
// 初始化数据库
DBHelper dbHelper = new DBHelper(getContext(), DB_NAME, null, DB_VERSION);
db = dbHelper.getWritableDatabase();
return true;
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
// 查询数据
Cursor cursor = db.query(TABLE_NAME, projection, selection, selectionArgs, null, null, sortOrder);
// 设置通知Uri,当这个Uri发生变化时,ContentObserver会收到通知
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
// 根据Uri返回对应的MIME类型
switch (uriMatcher.match(uri)) {
case URI_CODE_ALL:
return CONTENT_TYPE;
case URI_CODE_ONE:
return CONTENT_ITEM_TYPE;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
// 插入数据
long rowId = db.insert(TABLE_NAME, null, values);
if (rowId > 0) {
// 插入成功,返回新插入的数据的Uri
Uri newUri = ContentUris.withAppendedId(uri, rowId);
// 通知数据发生变化
getContext().getContentResolver().notifyChange(newUri, null);
return newUri;
} else {
// 插入失败
throw new SQLException("Failed to insert row into " + uri);
}
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
// 删除数据
int count = db.delete(TABLE_NAME, selection, selectionArgs);
if (count > 0) {
// 删除成功,通知数据发生变化
getContext().getContentResolver().notifyChange(uri, null);
}
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
// 更新数据
int count = db.update(TABLE_NAME, values, selection, selectionArgs);
if (count > 0) {
// 更新成功,通知数据发生变化
getContext().getContentResolver().notifyChange(uri, null);
}
return count;
}
// Uri匹配器
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int URI_CODE_ALL = 1;
private static final int URI_CODE_ONE = 2;
static {
uriMatcher.addURI("com.example.mycontentprovider", TABLE_NAME, URI_CODE_ALL);
uriMatcher.addURI("com.example.mycontentprovider", TABLE_NAME + "/#", URI_CODE_ONE);
}
}
```
ContentResolver:
```java
public class MyActivity extends Activity {
// ContentProvider的Uri
private static final Uri CONTENT_URI = Uri.parse("content://com.example.mycontentprovider/my_table");
// ContentProvider支持的MIME类型
private static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.com.example.mycontentprovider";
private static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.com.example.mycontentprovider";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// 查询数据
Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex("data"));
Log.d("MyActivity", "data: " + data);
}
cursor.close();
}
// 插入数据
ContentValues values = new ContentValues();
values.put("data", "Hello, ContentProvider!");
Uri newUri = getContentResolver().insert(CONTENT_URI, values);
Log.d("MyActivity", "newUri: " + newUri);
// 更新数据
values.put("data", "Hello, ContentResolver!");
int count = getContentResolver().update(newUri, values, null, null);
Log.d("MyActivity", "count: " + count);
// 删除数据
count = getContentResolver().delete(newUri, null, null);
Log.d("MyActivity", "count: " + count);
}
}
```
阅读全文