请举个例子
时间: 2023-10-10 21:12:59 浏览: 57
好的,以下是一个简单的例子,演示如何使用ContentProvider暴露SQLite数据库中的数据:
1. 创建一个继承自ContentProvider的类,例如MyContentProvider,并重写父类的方法:
```java
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase db;
@Override
public boolean onCreate() {
DBHelper dbHelper = new DBHelper(getContext());
db = dbHelper.getWritableDatabase();
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor = db.query("mytable", projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
return "vnd.android.cursor.dir/mytable";
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
long id = db.insert("mytable", null, values);
getContext().getContentResolver().notifyChange(uri, null);
return Uri.parse("content://com.example.mycontentprovider/mytable/" + id);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count = db.delete("mytable", selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int count = db.update("mytable", values, selection, selectionArgs);
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
}
```
2. 在AndroidManifest.xml文件中注册ContentProvider:
```xml
<application>
<provider
android:name=".MyContentProvider"
android:authorities="com.example.mycontentprovider"
android:exported="false"/>
</application>
```
3. 在ContentProvider的onCreate()方法中,初始化SQLite数据库,例如:
```java
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "mydb";
private static final int DB_VERSION = 1;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE mytable (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS mytable");
onCreate(db);
}
}
```
4. 在ContentProvider的query()、insert()、update()和delete()方法中,实现对数据的操作。例如,在query()方法中,查询mytable表中的所有数据,并返回Cursor:
```java
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor cursor = db.query("mytable", projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
```
5. 在ContentProvider的getType()方法中,指定返回的MIME类型。例如,返回vnd.android.cursor.dir/mytable表示返回的是一个包含多行数据的Cursor:
```java
@Nullable
@Override
public String getType(Uri uri) {
return "vnd.android.cursor.dir/mytable";
}
```
6. 在其他应用程序中,使用ContentResolver类访问ContentProvider提供的数据。例如,在Activity中,查询mytable表中的所有数据:
```java
Cursor cursor = getContentResolver().query(Uri.parse("content://com.example.mycontentprovider/mytable"), null, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
Log.d(TAG, "id: " + id + ", name: " + name);
}
cursor.close();
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)