ContentProvider作用:是用來對外暴露數據的。(在需要暴露數據的項目裏定義)
怎麼使用?
1、寫一個類 extends ContentProvider
在AndroidMenifest.xml文件application節點裏配置
<!-- android:authorities="" contentProvder的授權 相當於訪問路徑 -->
<provider
android:name=".provider.FishContentProvider"
android:authorities="com.ccc.sqlite.provider.FishContentProvider"/>
其他應用如何訪問ContentProvider?
需要用到這個類ContentResolver
還有 Uri
Uri的格式content://authorites (這裏uri格式已經固定好了,其中authorites是在暴露數據的項目裏定義好了)
注意先運行暴露數據的項目,在運行外部訪問的項目
public class FishContentProvider extends ContentProvider {
//添加可以訪問的uri,用到UriMatcher這個類
private static UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
//授權
private static final String authority = "com.ccc.sqlite.provider.FishContentProvider";
//匹配碼
private static final int FISH = 100;
private static final int FISHID = 101;
static{
//添加uri
matcher.addURI(authority, "fish", FISH);
//* # * 代表所有 # 代表數字
matcher.addURI(authority, "fish/#", FISHID);
}
private SQLiteOpenHelper mOpenHelper;
@Override
public boolean onCreate() {
// TODO Auto-generated method stub
Log.i("i", "我是ContentProvider ...我被創建了");
mOpenHelper = MySqliteOpenHelper.getInstance(getContext());
return false;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
// TODO Auto-generated method stub
Log.i("i", "我可以被別的應用訪問...");
//數據庫
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
Cursor ret = null;
//匹配Uri
int code = matcher.match(uri);
switch (code) {
case FISH:
ret = db.query("fish",
projection,
selection, selectionArgs, null, null, sortOrder);
break;
default:
throw new IllegalArgumentException("uri not found "+uri);
}
return ret;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
// TODO Auto-generated method stub
Uri returi = null;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int code = matcher.match(uri);
switch (code) {
case FISH:
long rowid = db.insert("fish", "_id", values);
//添加一個id
returi = ContentUris.withAppendedId(uri, rowid);
//發出改變通知
ContentResolver cr = getContext().getContentResolver();
cr.notifyChange(uri, null);
break;
default:
throw new IllegalArgumentException("uri not found "+uri);
}
return returi;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int rows = 0;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int code = matcher.match(uri);
switch (code) {
case FISH:
rows = db.delete("fish", selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("uri not found "+uri);
}
return rows;
}
@Override
public int update(Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int rows = 0;
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
int code = matcher.match(uri);
switch (code) {
case FISH:
rows = db.update("fish", values, selection, selectionArgs);
break;
default:
throw new IllegalArgumentException("uri not found "+uri);
}
return rows;
}
//with vnd.android.cursor.item 單條數據 vnd.android.cursor.dir/ 多條數據
@Override
public String getType(Uri uri) {
// TODO Auto-generated method stub
String type = null;
int code = matcher.match(uri);
switch (code) {
case FISH:
type = "vnd.android.cursor.dir/fish";//固定格式,這種代表多條數據
break;
case FISHID:
type = "vnd.android.cursor.item/fish";
break;
default:
break;
}
return type;
}
}
在另外一個項目裏
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void test(View v){
//1 獲取ContentResolver
ContentResolver cr = getContentResolver();
cr.query(Uri.parse("content://com.ccc.sqlite.provider.FishContentProvider"), null, null, null, null);
}
public void query(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.ccc.sqlite.provider.FishContentProvider/fish");
Cursor c = cr.query(uri,
new String[]{"*"},
null, null, null);
while(c.moveToNext()){
int _id = c.getInt(0);
String name = c.getString(1);
int money = c.getInt(2);
Log.i("i", "_id:"+_id+",name:"+name+",money:"+money);
}
c.close();
}
public void insert(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.ccc.sqlite.provider.FishContentProvider/fish");
ContentValues values = new ContentValues();
values.put("name", "蘋果");
values.put("money", 10000);
cr.insert(uri, values);
}
public void update(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.ccc.sqlite.provider.FishContentProvider/fish");
ContentValues values = new ContentValues();
values.put("money", 0);
cr.update(uri, values, "name = ?", new String[]{"蘋果"});
}
public void delete(View v){
ContentResolver cr = getContentResolver();
Uri uri = Uri.parse("content://com.ccc.sqlite.provider.FishContentProvider/fish");
cr.delete(uri, "name = ?", new String[]{"蘋果"});
}
}