contentprovider demo
时间: 2023-10-28 13:03:06 浏览: 58
ContentProvider是Android平台中的一种组件,用于不同应用之间共享数据。以下是一个ContentProvider的简单示例。
首先,在AndroidManifest.xml文件中注册ContentProvider。示例代码如下:
```
<provider
android:name=".MyContentProvider"
android:authorities="com.example.myapp.provider"
android:exported="true">
</provider>
```
然后,在项目中创建一个继承自ContentProvider的类。示例代码如下:
```java
public class MyContentProvider extends ContentProvider {
// 数据库帮助类
private MyDatabaseHelper dbHelper;
// ContentProvider创建时调用
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext());
return true;
}
// 查询数据
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("my_table", projection, selection, selectionArgs,
null, null, sortOrder);
return cursor;
}
// 插入数据
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
long id = db.insert("my_table", 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("my_table", values, selection, selectionArgs);
return count;
}
// 删除数据
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = db.delete("my_table", selection, selectionArgs);
return count;
}
// 返回MIME类型
@Override
public String getType(Uri uri) {
return "vnd.android.cursor.dir/vnd.com.example.myapp.provider.my_table";
}
}
```
在示例中,query方法用于查询数据,insert方法用于插入数据,update方法用于更新数据,delete方法用于删除数据。getType方法用于返回MIME类型。
最后,在应用中通过ContentResolver进行数据的增删改查操作。示例代码如下:
```java
// 查询数据
Uri uri = Uri.parse("content://com.example.myapp.provider/my_table");
Cursor cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
String data = cursor.getString(cursor.getColumnIndex("data"));
// 处理数据
}
cursor.close();
}
// 插入数据
Uri uri = Uri.parse("content://com.example.myapp.provider/my_table");
ContentValues values = new ContentValues();
values.put("data", "example data");
Uri newUri = getContentResolver().insert(uri, values);
// 更新数据
Uri uri = Uri.parse("content://com.example.myapp.provider/my_table");
ContentValues values = new ContentValues();
values.put("data", "new data");
String whereClause = "data=?";
String[] whereArgs = {"example data"};
int count = getContentResolver().update(uri, values, whereClause, whereArgs);
// 删除数据
Uri uri = Uri.parse("content://com.example.myapp.provider/my_table");
String whereClause = "data=?";
String[] whereArgs = {"example data"};
int count = getContentResolver().delete(uri, whereClause, whereArgs);
```
通过上述示例,你可以了解到ContentProvider的基本使用方法。它可以将数据存储在数据库中,并通过ContentResolver进行增删改查操作,实现数据的共享和交互。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)