Android 四大组件丨ContentProvider

ContentProvider 采用 Binder 机制,实现不同进程间共享数据与通信。本文模拟场景为:我拥有一张银行卡,家里收入归我统一管理!媳妇的收益转账给我,媳妇的零花钱由我转出,啧啧啧,我再做会儿梦!


应用 A

创建 ContentProvider

/**
 * @des ContentProvider 事件接收类
 * @author liyongli 20191108
 * */
public class MoneyProvider extends ContentProvider {

    // 定义 AUTHORITY (唯一路径)
    public static final String AUTHORITY = "com.myprovider.test.moneyprovider";

    // 定义过滤器
    private static UriMatcher uriMatcher;

    // 实例化本类时时调用
    static {
        uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        uriMatcher.addURI(AUTHORITY, "family", 0);
    }

    @Override
    public boolean onCreate() {
        return false;
    }

    @Nullable
    @Override
    public Uri insert(Uri uri,  @Nullable ContentValues values) {
        Log.e("==", "媳妇打钱了!");
        return null;
    }

    @Override
    public int delete(Uri uri,  @Nullable String selection,  @Nullable String[] selectionArgs) {
        Log.e("==", "媳妇要零花钱(全要)!");
        return 0;
    }

    @Override
    public int update(Uri uri,  @Nullable ContentValues values,  @Nullable String selection,  @Nullable String[] selectionArgs) {
        Log.e("==", "媳妇要零花钱(不全要)!");
        return 0;
    }

    
    @Nullable
    @Override
    public Cursor query(Uri uri,  @Nullable String[] projection,  @Nullable String selection,  @Nullable String[] selectionArgs,  @Nullable String sortOrder) {
        Log.e("==", "媳妇要查账!");
        return null;
    }

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

注册 ContentProvider

        
        
        

应用 B

Activity

/**
 * @des ContentProvider 事件发起类
 * @author liyongli 20191108
 * */
public class MainActivity extends AppCompatActivity {

    public static final String AUTHORITY = "com.myprovider.test.moneyprovider";

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

        findViewById(R.id.add).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://" + AUTHORITY +"/money");
                ContentValues values = new ContentValues();
                getContentResolver().insert(uri, values);
            }
        });

        findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://" + AUTHORITY +"/money");
                getContentResolver().delete(uri,"", new String[]{""});
            }
        });

        findViewById(R.id.update).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://" + AUTHORITY +"/money");
                ContentValues values = new ContentValues();
                getContentResolver().update(uri,values,"",new String[]{""});
            }
        });

        findViewById(R.id.query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Uri uri = Uri.parse("content://" + AUTHORITY +"/money");
                Cursor cursor = getContentResolver().query(uri,null,null,null,null);
            }
        });

    }

}

XML




        

运行效果

先运行应用 A,再运行应用 B

Android 四大组件丨ContentProvider_第1张图片

点击应用 A 按钮后,应用 B 控制台输出如下:

2019-11-08 17:36:31.053 E/==: 媳妇打钱了!
2019-11-08 17:36:31.685 E/==: 媳妇要零花钱(全要)!
2019-11-08 17:36:32.161 E/==: 媳妇要零花钱(不全要)!
2019-11-08 17:36:32.714 E/==: 媳妇要查账!

ContentProvider、ContentResolver、ContentObserver 之间的关系

ContentProvider:内容提供者。负责对外提供数据
ContentResolver:内容解析者。负责获取ContentProvider提供的数据
ContentObserver :内容监听器。监听数据的变化


待更新内容:

ContentProvider 过程原理及分析


本篇将持续更新 ContentProvider 相关知识,一起查漏补缺学个痛快!欢迎点赞留香丨留言鼓励丨指出不足!


Android 四大组件丨ContentProvider_第2张图片

你可能感兴趣的:(Java,For,Android)