学习好几年了,但是一直没有整理过自己学习的知识点,最近从中科创达离职了,现在复习下一下自己学习的内容。先从ContentProvider开始
android四大件 ContentProider/Service/Activity/BroadcaseReceiver
ContentProvider是android四大件之一,也是在数据存储中应用比较多的一个,主要是用来对面提供数据服务。
public abstract @Nullable String getType(@NonNull Uri uri);
public abstract @Nullable Uri insert(@NonNull Uri uri, @Nullable ContentValues values);
public abstract int update(@NonNull Uri uri, @Nullable ContentValues values,
@Nullable String selection, @Nullable String[] selectionArgs);
public abstract int delete(@NonNull Uri uri, @Nullable String selection,
@Nullable String[] selectionArgs);
public abstract @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection,
@Nullable String selection, @Nullable String[] selectionArgs,
@Nullable String sortOrder);
定义Uri的类型型,并且在getType中返回定影的类型
vnd.android.cursor.item/content_uri
vnd.android.cursor.dir/content_uri
对外服务的Content_Uri ,需要注意在ContentProvider中以及AndroidManifest中声明这个provider。这个contentUri就像网址一样,当提供uri以后系统就会查找到对应的这ContentProvider
<provider
android:authorities="com.kunlamp.bookmanager"
android:name=".BookProvider"/>
使用ContentProvider时候需要注意更新查找数据时候提供对应的条件以及参数
public abstract @Nullable Cursor query(@NonNull Uri uri, @Nullable String[] projection,
@Nullable String selection, @Nullable String[] selectionArgs,
uri 是query时候的uri
projection 表结构对应的列
selection和selectionArgs是查询条件 也是sql语句中的where条件
添加了一个自定义的contentprovider文件,可以用来参考一下
package note.com.notemanager;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.Log;
/**
* Created by ts on 2/15/17.
*/
public class BookProvider extends ContentProvider {
private final String TAG = "BookProvider";
static public final String mAuthority = "com.kunlamp.bookmanager";
static public final String BOOKPATH = "book/#";
static public final String BOOKCOLLECTONPATH = "book";
// static public final String CONTENT_URI = mAuthority + BOOKCOLLECTONPATH;
static public final Uri CONTENT_URI = Uri.parse("content://" + mAuthority + "/" + BOOKCOLLECTONPATH);
static final private int BOOK = 2;
static final private int BOOKCOLLECTION = 3;
private static final UriMatcher mBookMatcher = new UriMatcher(UriMatcher.NO_MATCH);
static {
mBookMatcher.addURI(mAuthority, BOOKCOLLECTONPATH, BOOKCOLLECTION); //Muti
mBookMatcher.addURI(mAuthority, BOOKPATH, BOOK); //Signle
}
BookDatabaseHelper mBookDbHelper = null;
final int ERROR = -1;
@Override
public boolean onCreate() {
mBookDbHelper = new BookDatabaseHelper(this.getContext());
return false;
}
@Nullable
@Override
public Cursor query(Uri uri, String[] projections, String selection, String[] selectionArgs, String sortOrder) {
SQLiteDatabase db = mBookDbHelper.getWritableDatabase();
db.beginTransaction();
int matchId = mBookMatcher.match(uri);
Log.d(TAG, " sssss query cursor = " + matchId);
Cursor cursor = null;
switch (matchId) {
case BOOK: {
} break;
case BOOKCOLLECTION: {
// cursor =db.rawQuery("select * from " + BookInfo.TABLE_BOOK + ";", null);
cursor =db.query(BookInfo.TABLE_BOOK,projections,selection,selectionArgs,null,null,null);
}
default:
}
db.endTransaction();
Log.d(TAG, " sssss query cursor = " + cursor);
return cursor;
}
@Nullable
@Override
public String getType(Uri uri) {
switch (mBookMatcher.match(uri)) {
case BOOK: {
return "vnd.android.cursor.item/vnd.bookmanager.book";
}
case BOOKCOLLECTION: {
return "vnd.android.cursor.dir/vnd.bookmanager.book";
}
}
return null;
}
@Nullable
@Override
public Uri insert(Uri uri, ContentValues contentValues) {
Log.d(TAG, " sssssinsert uri = " + uri);
Log.d(TAG, " sssss contentValues = " + contentValues);
SQLiteDatabase db = mBookDbHelper.getWritableDatabase();
long insert_id = db.insert("book", null, contentValues);
// String sql =" insert into " + BookInfo.TABLE_BOOK + "( "+ BookInfo.BOOKID + "," + BookInfo.BOOKNAME + ")" +
// "values(\"" + contentValues.getAsString(BookInfo.BOOKID) + "\",\"" +contentValues.getAsString(BookInfo.BOOKNAME) + "\");";
// Log.d(TAG, " sssss insert sql = " + sql);
//
// db.rawQuery(sql, null);
Log.d(TAG, " sssss insert_id = " + insert_id);
Uri result = ContentUris.withAppendedId(uri,insert_id);
getContext().getContentResolver().notifyChange(result, null);
Log.d(TAG, " sssss result = " + result);
return result;
}
@Override
public int delete(Uri uri, String whereClause, String[] whereArgs) {
Log.d(TAG, " delete uri = " + uri + "whereClause = " + whereClause + " , whereArgs = " + whereArgs);
SQLiteDatabase db = mBookDbHelper.getWritableDatabase();
return db.delete(BookInfo.TABLE_BOOK,whereClause,whereArgs);
}
@Override
public int update(Uri uri, ContentValues contentValues, String whereClause, String[] whereArgs) {
Log.d(TAG, " update contentValues = " + contentValues + "whereClause = " + whereClause + " , whereArgs = " + whereArgs);
SQLiteDatabase db = mBookDbHelper.getWritableDatabase();
int count = db.update(BookInfo.TABLE_BOOK,contentValues,whereClause,whereArgs);
Log.d(TAG, " update count = " + count);
return count;
}
}