android studio 内容提供者例子
时间: 2023-11-16 13:57:24 浏览: 35
Android Studio中的内容提供者示例可以通过以下步骤实现:
1. 创建一个新项目并在其中创建一个新的Java类。
2. 在Java类中实现ContentProvider类,并重写其方法以提供数据的增删改查功能。
3. 在AndroidManifest.xml文件中注册ContentProvider类。
4. 在应用程序中使用ContentResolver对象来访问ContentProvider中的数据。
以下是一个简单的示例代码,演示如何创建一个ContentProvider类并实现其方法:
```
public class MyContentProvider extends ContentProvider {
private static final String AUTHORITY = "com.example.myapp.provider";
private static final String BASE_PATH = "mydata";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + BASE_PATH);
private SQLiteDatabase database;
@Override
public boolean onCreate() {
DBHelper dbHelper = new DBHelper(getContext());
database = dbHelper.getWritableDatabase();
return true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
queryBuilder.setTables(MyTable.TABLE_NAME);
int uriType = sURIMatcher.match(uri);
switch (uriType) {
case MYDATA:
break;
case MYDATA_ID:
queryBuilder.appendWhere(MyTable.COLUMN_ID + "=" + uri.getLastPathSegment());
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
Cursor cursor = queryBuilder.query(database, projection, selection, selectionArgs, null, null, sortOrder);
cursor.setNotificationUri(getContext().getContentResolver(), uri);
return cursor;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
int uriType = sURIMatcher.match(uri);
long id = 0;
switch (uriType) {
case MYDATA:
id = database.insert(MyTable.TABLE_NAME, null, values);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return Uri.parse(BASE_PATH + "/" + id);
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
int rowsDeleted = 0;
switch (uriType) {
case MYDATA:
rowsDeleted = database.delete(MyTable.TABLE_NAME, selection, selectionArgs);
break;
case MYDATA_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsDeleted = database.delete(MyTable.TABLE_NAME, MyTable.COLUMN_ID + "=" + id, null);
} else {
rowsDeleted = database.delete(MyTable.TABLE_NAME, MyTable.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsDeleted;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
int uriType = sURIMatcher.match(uri);
int rowsUpdated = 0;
switch (uriType) {
case MYDATA:
rowsUpdated = database.update(MyTable.TABLE_NAME, values, selection, selectionArgs);
break; case MYDATA_ID:
String id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsUpdated = database.update(MyTable.TABLE_NAME, values, MyTable.COLUMN_ID + "=" + id, null);
} else {
rowsUpdated = database.update(MyTable.TABLE_NAME, values, MyTable.COLUMN_ID + "=" + id + " and " + selection, selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsUpdated;
}
private static final UriMatcher sURIMatcher = new UriMatcher(UriMatcher.NO_MATCH);
private static final int MYDATA = 1;
private static final int MYDATA_ID = 2;
static {
sURIMatcher.addURI(AUTHORITY, BASE_PATH, MYDATA);
sURIMatcher.addURI(AUTHORITY, BASE_PATH + "/#", MYDATA_ID);
}
}
```
在这个示例中,我们创建了一个名为MyContentProvider的ContentProvider类,并实现了其query、insert、delete和update方法。我们还在AndroidManifest.xml文件中注册了MyContentProvider类,并在应用程序中使用ContentResolver对象来访问ContentProvider中的数据。