Android四大组件-ContentProvide

2021-08-17  本文已影响0人  雷根儿

Android四大组件ActivityServiceBroadcastReceiverContentProvide

四、ContentProvide

1.什么是ContentProvide
ContentProvider是Android中提供的专门用于不同应用间数据交互和共享的组件。其本质上是一个标准化的数据管道,它屏蔽了底层的数据管理和服务等细节,以标准化的方式在Android 应用间共享数据、数据交互,跨进程通信。

2.使用方法
1、在当前应用自定义ContentProvider类

public class MyContentProvide extends ContentProvider {
    //这里的AUTHORITY就是我们在AndroidManifest.xml中配置的authorities
    private static final String AUTHORITY = "com.test.MyProvider";
    //匹配成功后的匹配码
    private static final int MATCH_CODE = 100;
    private static UriMatcher uriMatcher;
    //数据改变后指定通知的Uri
    private static final Uri NOTIFY_URI = Uri.parse("content://" + AUTHORITY );

    static {
        //匹配不成功返回NO_MATCH(-1)
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //添加我们需要匹配的uri
        uriMatcher.addURI(AUTHORITY,"", MATCH_CODE);
    }
    @Override
    public boolean onCreate() {
        SQLiteDatabase db = this.getContext().openOrCreateDatabase("test_db.db3", Context.MODE_PRIVATE, null);
        db.execSQL("create table tab(_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)");
        ContentValues values = new ContentValues();
        values.put("name", "test");
        db.insert("tab", "_id", values);
        db.close();
        return true;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] strings, @Nullable String s, @Nullable String[] strings1, @Nullable String s1) {
        int match = uriMatcher.match(uri);
        if (match == MATCH_CODE){
            SQLiteDatabase db = this.getContext().openOrCreateDatabase("test_db.db3", Context.MODE_PRIVATE, null);
            Cursor c = db.query("tab", null, null, null, null, null,null);
            return c;
        }
        return null;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues contentValues) {
        return null;
    }

    @Override
    public int delete(@NonNull Uri uri, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues contentValues, @Nullable String s, @Nullable String[] strings) {
        return 0;
    }
}

2、在AndroidManifest.xml中进行注册

<provider android:name=".activity.MyContentProvide" android:authorities="com.test.MyProvider"/>

3、其他应用使用ContentResolver对数据进行CRUD操作

public class MyContentResolver extends BaseActivity {
    private ContentResolver resolver;
    private Observer observer;
    @Override
    protected void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //获取ContentResolver对象
        resolver = getContentResolver();
        //获取Uri对象
        Uri uri = Uri.parse("content://com.test.MyProvider");
        //获取数据
        Cursor c = resolver.query(uri, null, null, null, null);
        c.moveToFirst();
        for (int i = 0; i < c.getCount(); i++) {
            int index = c.getColumnIndexOrThrow("name");
            String src = c.getString(index);
            Log.d("TAG", src);
            c.moveToNext();
        }
        //注册ContentObserver监听数据的变化
        observer = new Observer(new Handler());
        resolver.registerContentObserver(uri, true,observer);
    }
    class Observer extends ContentObserver {

        public Observer(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Toast.makeText(MyContentResolver.this,
                    "数据列表发生变化", Toast.LENGTH_SHORT).show();
            //onchange 方法中添加Toast便于观察
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        resolver.unregisterContentObserver(observer);
    }
}

3.其他相关类

4.应用场景

上一篇 下一篇

猜你喜欢

热点阅读