android contprovider 案例
时间: 2023-10-27 22:04:55 浏览: 68
Android的ContentProvider是一种数据共享机制,可以让应用程序之间共享数据。下面是一个简单的ContentProvider案例,演示如何创建和使用ContentProvider。
1. 创建ContentProvider类
```java
public class MyContentProvider extends ContentProvider {
private MyDatabaseHelper dbHelper;
private static final UriMatcher uriMatcher;
private static final int PERSONS = 1;
private static final int PERSON_ID = 2;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI("com.example.myapp.provider", "persons", PERSONS);
uriMatcher.addURI("com.example.myapp.provider", "persons/#", PERSON_ID);
}
@Override
public boolean onCreate() {
dbHelper = new MyDatabaseHelper(getContext(), "myapp.db", null, 1);
return true;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = null;
switch (uriMatcher.match(uri)) {
case PERSONS:
cursor = db.query("persons", projection, selection, selectionArgs, null, null, sortOrder);
break;
case PERSON_ID:
String id = uri.getPathSegments().get(1);
cursor = db.query("persons", projection, "id=?", new String[]{id}, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case PERSONS:
return "vnd.android.cursor.dir/persons";
case PERSON_ID:
return "vnd.android.cursor.item/person";
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues values) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
Uri uriReturn = null;
switch (uriMatcher.match(uri)) {
case PERSONS:
case PERSON_ID:
long newId = db.insert("persons", null, values);
uriReturn = Uri.parse("content://com.example.myapp.provider/persons/" + newId);
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
return uriReturn;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = 0;
switch (uriMatcher.match(uri)) {
case PERSONS:
count = db.delete("persons", selection, selectionArgs);
break;
case PERSON_ID:
String id = uri.getPathSegments().get(1);
count = db.delete("persons", "id=?", new String[]{id});
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
SQLiteDatabase db = dbHelper.getWritableDatabase();
int count = 0;
switch (uriMatcher.match(uri)) {
case PERSONS:
count = db.update("persons", values, selection, selectionArgs);
break;
case PERSON_ID:
String id = uri.getPathSegments().get(1);
count = db.update("persons", values, "id=?", new String[]{id});
break;
default:
throw new IllegalArgumentException("Unknown URI: " + uri);
}
return count;
}
}
```
2. 创建数据库类
```java
public class MyDatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_PERSONS = "create table persons ("
+ "id integer primary key autoincrement, "
+ "name text, "
+ "age integer)";
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_PERSONS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
```
3. 在AndroidManifest.xml中添加ContentProvider
```xml
<provider
android:name=".MyContentProvider"
android:authorities="com.example.myapp.provider"
android:exported="true" />
```
4. 使用ContentProvider
```java
Uri uri = Uri.parse("content://com.example.myapp.provider/persons");
ContentResolver resolver = getContentResolver();
Cursor cursor = resolver.query(uri, new String[]{"id", "name", "age"}, null, null, null);
if (cursor != null) {
while (cursor.moveToNext()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int age = cursor.getInt(cursor.getColumnIndex("age"));
Log.d(TAG, "id=" + id + ", name=" + name + ", age=" + age);
}
cursor.close();
}
```
以上就是一个简单的ContentProvider案例,通过这个案例可以学习如何创建和使用ContentProvider。
阅读全文