美文网首页
Android四大组件之内容提供者

Android四大组件之内容提供者

作者: kdroid | 来源:发表于2016-07-29 17:40 被阅读67次

    ContentProvider
    是Android中提供的专门用于不同应用之间进行数据共享的方式
    底层由Binder机制实现,由于系统对他做了封装,使用起来比AIDL方便许多,无需关注底层细节即可轻松进行IPC
    Provider的onCreate由Server端主线程调用,所以不要做耗时任务
    CRUD操作由Binder线程调用,所以可以做并发操作,不会引起堵塞

    Server


    // 服务端数据库
    public class DbOpenHelper extends SQLiteOpenHelper {
        private static final String DB_NAME = "providers.db";
        private static final int DB_VERSION = 1;
    
        public static final String BOOK_TABLE_NAME = "book";
        public static final String USER_TABLE_NAME = "user";
    
        private static final String CREATE_BOOK_TABLE = "CREATE TABLE IF NOT EXISTS " + BOOK_TABLE_NAME + "(" +
                "_id    INTEGER PRIMARY KEY, " +
                "name   TEXT" +
                ")";
    
        private static final String CREATE_USER_TABLE = "CREATE TABLE IF NOT EXISTS " + USER_TABLE_NAME + "(" +
                "_id    INTEGER PRIMARY KEY, " +
                "name   TEXT," +
                "sex    INTEGER" +
                ")";
    
    
        public DbOpenHelper(Context context) {
            super(context, DB_NAME, null, DB_VERSION);
        }
    
    
        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(CREATE_BOOK_TABLE);
            db.execSQL(CREATE_USER_TABLE);
        }
    
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO: 2016/7/29 ignore
        }
    }
    
    // 实现内容提供者
    public class CustomProvider extends ContentProvider {
        public static final String TAG = "CustomProvider";
        public static final String AUTHORITY = "com.rtfsc.auth.provider";
    
        public static final Uri BOOK_CONTENT_URI = Uri.parse("content://"
                + AUTHORITY + "/book");
        public static final Uri USER_CONTENT_URI = Uri.parse("content://"
                + AUTHORITY + "/user");
    
        public static final int BOOK_URI_CODE = 0;
        public static final int USER_URI_CODE = 1;
    
        private static final UriMatcher sUriMatcher = new UriMatcher(
                UriMatcher.NO_MATCH);
    
        private Context mContext;
        private SQLiteDatabase mDatabase;
    
        static {
            sUriMatcher.addURI(AUTHORITY, "book", BOOK_URI_CODE);
            sUriMatcher.addURI(AUTHORITY, "user", USER_URI_CODE);
        }
    
        private String getTableName(Uri uri) {
            String tableName = null;
            switch (sUriMatcher.match(uri)) {
                case BOOK_URI_CODE:
                    tableName = DbOpenHelper.BOOK_TABLE_NAME;
                    break;
                case USER_URI_CODE:
                    tableName = DbOpenHelper.USER_TABLE_NAME;
                    break;
                default:
                    break;
            }
            return tableName;
        }
    
        @Override
        public boolean onCreate() {
            Log.d(TAG, "onCreate, current thread: " + Thread.currentThread().getName());
            mContext = getContext();
            mDatabase = new DbOpenHelper(mContext).getWritableDatabase();
            initData();
            return true;
        }
    
        private void initData() {
            mDatabase.execSQL("delete from " + DbOpenHelper.BOOK_TABLE_NAME);
            mDatabase.execSQL("delete from " + DbOpenHelper.USER_TABLE_NAME);
    
            mDatabase.execSQL("insert into " + DbOpenHelper.BOOK_TABLE_NAME + " values(1,'Android');");
            mDatabase.execSQL("insert into " + DbOpenHelper.BOOK_TABLE_NAME + " values(2,'iOS');");
            mDatabase.execSQL("insert into " + DbOpenHelper.BOOK_TABLE_NAME + " values(3,'Pytnon');");
    
            mDatabase.execSQL("insert into " + DbOpenHelper.USER_TABLE_NAME + " values(1,'Wong',1);");
            mDatabase.execSQL("insert into " + DbOpenHelper.USER_TABLE_NAME + " values(2,'Kim',1);");
            mDatabase.execSQL("insert into " + DbOpenHelper.USER_TABLE_NAME + " values(3,'Hung',1);");
        }
    
        @Nullable
        @Override
        public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
            Log.d(TAG, "query, current thread: " + Thread.currentThread().getName());
            String tableName = getTableName(uri);
            if (tableName.isEmpty()) {
                throw new IllegalArgumentException("Unsupport URI: " + uri);
            }
            return mDatabase.query(tableName, projection, selection, selectionArgs, null, null, null);
        }
    
        @Nullable
        @Override
        public String getType(Uri uri) {
            Log.d(TAG, "getType");
            return null;
        }
    
        @Nullable
        @Override
        public Uri insert(Uri uri, ContentValues values) {
            Log.d(TAG, "insert");
            String tableName = getTableName(uri);
            if (tableName.isEmpty()) {
                throw new IllegalArgumentException("Unsupport URI: " + uri);
            }
            mDatabase.insert(tableName, null, values);
            mContext.getContentResolver().notifyChange(uri, null);
            return uri;
        }
    
        @Override
        public int delete(Uri uri, String selection, String[] selectionArgs) {
            Log.d(TAG, "delete");
            String tableName = getTableName(uri);
            if (tableName.isEmpty()) {
                throw new IllegalArgumentException("Unsupport URI: " + uri);
            }
            int count = mDatabase.delete(tableName, selection, selectionArgs);
            if (count > 0) {
                mContext.getContentResolver().notifyChange(uri, null);
            }
            return count;
        }
    
        @Override
        public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
            Log.d(TAG, "update");
            String tableName = getTableName(uri);
            if (tableName.isEmpty()) {
                throw new IllegalArgumentException("Unsupport URI: " + uri);
            }
            int row = mDatabase.update(tableName, values, selection, selectionArgs);
            if (row > 0) {
                mContext.getContentResolver().notifyChange(uri, null);
            }
            return row;
        }
    }
    
    //  注册内容提供者
    <provider
                android:name="com.rtfsc.CustomProvider"
                android:authorities="com.rtfsc.auth.provider"
                android:permission="com.rtfsc.provider"
                android:process=":provider"/>
    

    Client


    //  添加权限
    <uses-permission android:name="com.rtfsc.provider"/>
    
    public class MainActivity extends AppCompatActivity {
    
        public static final String PROVIDER_CONTENT_PREFIX = "content://com.rtfsc.auth.provider";
    
        private IOnChangeListener mListener = new IOnChangeListener.Stub() {
            @Override
            public void onChange(String aString) throws RemoteException {
                System.out.println(aString);
            }
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Uri bookUri = Uri.parse(PROVIDER_CONTENT_PREFIX + "/book");
            Uri userUri = Uri.parse(PROVIDER_CONTENT_PREFIX + "/user");
    
            ContentValues contentValues = new ContentValues();
            contentValues.put("_id", 4);
            contentValues.put("name", "Effctive of java");
    
            getContentResolver().insert(bookUri, contentValues);
    
            Cursor cursor = getContentResolver().query(bookUri, new String[]{"_id", "name"}, null, null, null);
            if (cursor == null) {
                return;
            }
            while (cursor.moveToNext()) {
                String id = cursor.getString(cursor.getColumnIndex("_id"));
                String name = cursor.getString(cursor.getColumnIndex("name"));
                System.out.println("id = " + id + " name = " + name);
            }
            cursor.close();
        }
    }
    
    //  log
    07-29 09:28:34.885 1277-1277/com.rtfsc I/System.out: id = 1 name = Android
    07-29 09:28:34.885 1277-1277/com.rtfsc I/System.out: id = 2 name = iOS
    07-29 09:28:34.885 1277-1277/com.rtfsc I/System.out: id = 3 name = Pytnon
    07-29 09:28:34.885 1277-1277/com.rtfsc I/System.out: id = 4 name = Effctive of java
    

    相关文章

      网友评论

          本文标题:Android四大组件之内容提供者

          本文链接:https://www.haomeiwen.com/subject/fqmzjttx.html