Android 内容提供者ContentProvider

public class PersonDBProvider extends ContentProvider {

    private PersonDBHelper mDbHelper;

    //定义匹配工具,如果不匹配返回-1;
    private static UriMatcher mMatcher = new UriMatcher(UriMatcher.NO_MATCH);
    private static final String AUTHORITIES = "com.android.basic.provider.PersonDBProvider";
    private static final int INSERT = 0;
    private static final int DELETE = 1;
    private static final int UPDATE = 2;
    private static final int QUERY = 3;
    private static Uri PERSONS_DB_URI = Uri.parse("content://" + AUTHORITIES);

    private static final String TABLE = "person";

    static{
        mMatcher.addURI(AUTHORITIES, "insert", INSERT);
        mMatcher.addURI(AUTHORITIES, "delete", DELETE);
        mMatcher.addURI(AUTHORITIES, "update", UPDATE);
        mMatcher.addURI(AUTHORITIES, "query", QUERY);
    }

    @Override
    public boolean onCreate() {
        mDbHelper = new PersonDBHelper(getContext());
        return false;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
            String[] selectionArgs, String sortOrder) {
        if (mMatcher.match(uri)==QUERY) {
            SQLiteDatabase db = mDbHelper.getReadableDatabase();
            return db.query(TABLE, projection, selection, selectionArgs, null, null, sortOrder);
        } else {
            throw new IllegalArgumentException("路径错误!");
        }
    }

    @Override
    public String getType(Uri uri) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        if (mMatcher.match(uri) == INSERT) {
            Logger.i("tag", "uri==" + uri.toString());
            Logger.i("tag", "getAuthority==" + uri.getAuthority());
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            long index = db.insert(TABLE, null, values);
            getContext().getContentResolver().notifyChange(PERSONS_DB_URI, null);
            return Uri.withAppendedPath(uri, index+"");
        }else {
            throw new IllegalArgumentException("路径错误!");
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        if (mMatcher.match(uri) == DELETE) {
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            getContext().getContentResolver().notifyChange(PERSONS_DB_URI, null);
            return db.delete(TABLE, selection, selectionArgs);
        }else {
            throw new IllegalArgumentException("路径错误!");
        }
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
            String[] selectionArgs) {
        if (mMatcher.match(uri) == UPDATE) {
            SQLiteDatabase db = mDbHelper.getWritableDatabase();
            getContext().getContentResolver().notifyChange(PERSONS_DB_URI, null);
            return db.update(TABLE, values, selection, selectionArgs);
        }else {
            throw new IllegalArgumentException("路径错误!");
        }
    }

}

实现

public class ProviderActivity extends Activity {

    private static final Uri QUERY_URI = Uri.parse("content://com.android.basic.provider.PersonDBProvider/query");
    private static final Uri ADD_URI = Uri.parse("content://com.android.basic.provider.PersonDBProvider/add");
    private static final Uri DELETE_URI = Uri.parse("content://com.android.basic.provider.PersonDBProvider/delete");
    private static final Uri UPDATE_URI = Uri.parse("content://com.android.basic.provider.PersonDBProvider/update");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_provider);
    }

    public void query(View view){
        Cursor cursor = getContentResolver().query(QUERY_URI, null, null, null, null);
        while(cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String number = cursor.getString(cursor.getColumnIndex("number"));
            Logger.i("tag", "name==" + name);
            Logger.i("tag", "number==" + number);
        }
    }

    public void add(View view){
        for (int i = 0; i < 10; i++) {
            ContentValues values = new ContentValues();
            values.put("name", "tom"+i);
            values.put("number", "20"+i);
            Uri uri = getContentResolver().insert(ADD_URI, values);
        }
    }

    public void delete(View view){
        ContentValues values = new ContentValues();
        values.put("name", "tom");
        values.put("number", "20");
        int delete = getContentResolver().delete(DELETE_URI, "_id = ?", new String[]{"1"});
        Logger.i("tag", "delete==" + delete);
    }

    public void update(View view){
        ContentValues values = new ContentValues();
        values.put("name", "jack");
        values.put("number", "50");
        getContentResolver().update(UPDATE_URI, values, "_id = ?", new String[]{"1"});
    }
}

你可能感兴趣的:(android,内容提供)