android contentprovider作用,Android ContentProvider基本使用
一、基本概念:
1.ContentProvider為存儲和獲取數(shù)據(jù)提供了統(tǒng)一的接口;
2.使用ContentProvider可以在不同的應(yīng)用程序之間共享數(shù)據(jù);
3.Android為常見的一些數(shù)據(jù)提供了ContentProvider(包括音頻、視頻、圖片和通訊錄等等 )
直接貼下代碼:
定義數(shù)據(jù)庫幫助類:
public class DbOpenHelper extends SQLiteOpenHelper {
//數(shù)據(jù)庫名稱
private static final String DATA_BASE_NAME = "people.db";
//數(shù)據(jù)庫版本號
private static final int DATE_BASE_VERSION = 1;
//表名-男孩
public static final String BOY_TABLE_NAME = "boy";
//表名-女孩
public static final String GIRL_TABLE_NAME = "girl";
//創(chuàng)建表-男孩(兩列:主鍵自增長、姓名)
private final String CREATE_BOY_TABLE = "create table " + BOY_TABLE_NAME + "(_id integer primary key autoincrement, name text)";
//創(chuàng)建表-女孩(兩列:主鍵自增長、姓名)
private final String CREATE_GIRL_TABLE = "create table " + GIRL_TABLE_NAME + "(_id integer primary key autoincrement, name text)";
public DbOpenHelper(Context context) {
super(context, DATA_BASE_NAME, null, DATE_BASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_BOY_TABLE);//創(chuàng)建男孩表
db.execSQL(CREATE_GIRL_TABLE);//創(chuàng)建女孩表
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//一些數(shù)據(jù)庫升級操作
}
}
內(nèi)容提供者:
public class MyFirstContentProvider extends ContentProvider {
private Context context;
private SQLiteDatabase sqLiteDatabase;
public static final String AUTHORITY = "com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider";
public static final int BOY_URI_CODE = 0;
public static final int GIRL_URI_CODE = 1;
private static final UriMatcher uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
uriMatcher.addURI(AUTHORITY, DbOpenHelper.BOY_TABLE_NAME, BOY_URI_CODE);
uriMatcher.addURI(AUTHORITY, DbOpenHelper.GIRL_TABLE_NAME, GIRL_URI_CODE);
}
/**
* 獲取表名
* @param uri
* @return
*/
private String getTableName(Uri uri) {
String tableName = null;
switch (uriMatcher.match(uri)) {
case BOY_URI_CODE:
tableName = DbOpenHelper.BOY_TABLE_NAME;
break;
case GIRL_URI_CODE:
tableName = DbOpenHelper.GIRL_TABLE_NAME;
break;
}
return tableName;
}
@Override
public boolean onCreate() {
context = getContext();
initProviderData();
return false;
}
//初始化原始數(shù)據(jù)
private void initProviderData() {
sqLiteDatabase = new DbOpenHelper(context).getWritableDatabase();
sqLiteDatabase.beginTransaction();
ContentValues contentValues = new ContentValues();
contentValues.put("name", "男孩1");
sqLiteDatabase.insert(DbOpenHelper.BOY_TABLE_NAME, null, contentValues);
contentValues.put("name", "男孩2");
sqLiteDatabase.insert(DbOpenHelper.BOY_TABLE_NAME, null, contentValues);
contentValues.put("name", "男孩3");
sqLiteDatabase.insert(DbOpenHelper.BOY_TABLE_NAME, null, contentValues);
contentValues.clear();
contentValues.put("name", "女孩1");
sqLiteDatabase.insert(DbOpenHelper.GIRL_TABLE_NAME, null, contentValues);
contentValues.put("name", "女孩2");
sqLiteDatabase.insert(DbOpenHelper.GIRL_TABLE_NAME, null, contentValues);
contentValues.put("name", "女孩3");
sqLiteDatabase.insert(DbOpenHelper.GIRL_TABLE_NAME, null, contentValues);
sqLiteDatabase.setTransactionSuccessful();
sqLiteDatabase.endTransaction();
}
@Nullable
@Override
public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
String tableName = getTableName(uri);
if (TextUtils.isEmpty(tableName)) {
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
return sqLiteDatabase.query(tableName, projection, selection, selectionArgs, null, null, sortOrder);
}
@Nullable
@Override
public String getType(@NonNull Uri uri) {
return null;
}
@Nullable
@Override
public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {
String tableName = getTableName(uri);
if(TextUtils.isEmpty(tableName)){
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
sqLiteDatabase.insert(tableName, null, values);
context.getContentResolver().notifyChange(uri, null);
return uri;
}
@Override
public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
String tableName = getTableName(uri);
if (TextUtils.isEmpty(tableName)) {
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
int count = sqLiteDatabase.delete(tableName, selection, selectionArgs);
if (count > 0) {
context.getContentResolver().notifyChange(uri, null);
}
return count;
}
@Override
public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
String tableName = getTableName(uri);
if (TextUtils.isEmpty(tableName)) {
throw new IllegalArgumentException("Unsupported URI:" + uri);
}
int row = sqLiteDatabase.update(tableName, values, selection, selectionArgs);
if (row > 0) {
context.getContentResolver().notifyChange(uri, null);
}
return row;
}
}
注冊內(nèi)容提供者:
android:name=".MyFirstContentProvider"
android:exported="true"
android:authorities="com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider" />
使用:
Uri boyUri = Uri.parse("content://com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider/boy");
ContentValues contentValues = new ContentValues();
contentValues.put("name", "張三");
//getContentResolver().insert(boyUri, contentValues);
//getContentResolver().delete(boyUri, )
Cursor boyCursor = getContentResolver().query(boyUri, new String[]{"_id", "name"}, null, null, null);
if (boyCursor != null) {
while (boyCursor.moveToNext()) {
Log.e("yunchong", "ID:" + boyCursor.getInt(boyCursor.getColumnIndex("_id")) + " name:" + boyCursor.getString(boyCursor.getColumnIndex("name")));
}
boyCursor.close();
}
Uri girlUri = Uri.parse("content://com.zyc.hezuo.contentproviderdemo.MyFirstContentProvider/girl");
contentValues.clear();
//contentValues.put("name", "李四");
//getContentResolver().insert(girlUri, contentValues);
Cursor girlCursor = getContentResolver().query(girlUri, new String[]{"_id", "name"}, null, null, null);
if (girlCursor != null) {
while (girlCursor.moveToNext()) {
Log.e("yunchong", "ID:" + girlCursor.getInt(girlCursor.getColumnIndex("_id"))
+ " name:" + girlCursor.getString(girlCursor.getColumnIndex("name")));
}
girlCursor.close();
}
由于時(shí)間比較倉促,后續(xù)會繼續(xù)更新!
總結(jié)
以上是生活随笔為你收集整理的android contentprovider作用,Android ContentProvider基本使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解析EDA系统集成方案设计
- 下一篇: 基于Android实现的OA办公自动化系