ContentProvider的使用

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[]{"苹果"});
    }
}


 

发布了32 篇原创文章 · 获赞 4 · 访问量 1万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章