ContentProvider使用详解

ContentProvider简介
每一个Android应用程序就是一个进程,都是一个单独的虚拟机,他们之间也可能会有通信的需求,比如短信程序的联系人选择需要用到通讯录的联系人功能,ContentProvider和ContentResolver搭配使用,ContentProvider负责以一定规则暴露数据,ContentResolver负责把属于按照规则读取出来。

ContentProvider实现步骤

  • 定义继承自ContentProvider类
  • 注册ContentProvider,在AndroidManifest.xml文件中注册。

ContentProvider具体实现
ContentProvider类似于数据库中的表,有URL操作地址,有CURD操作。
ContentResolver也和ContentProvider相似,有相同属性操作。
他们通过同一URL实现数据交互。比如应用A通过ContentProvider暴露出自己的数据,应用B通过ContentResolver的URL与A联系,然后自己的curd操作通过URL传递给A,A再传递给系统来实现对A数据的curd操作。

自定义ContentProvider类:

public class MyContentProvider extends ContentProvider {
    //这里我们定义string模仿数据的改变
    String s ="";
    @Override
    public boolean onCreate() {
        Log.v("test","onCreate");
        return true;
    }

    @Override
    public Cursor query(Uri uri,String[] projection,String selection,String[] selectionArgs,String sortOrder) {
        Log.v("test","query"+",操作参数为:"+selection+"结果为:"+s);
        return null;
    }

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

    @Override
    public Uri insert(Uri uri,ContentValues values) {
        Log.v("test","insert"+",操作参数为:"+values);
        s = values.toString();
        return null;
    }

    @Override
    public int delete(Uri uri,String selection,String[] selectionArgs) {
        Log.v("test","delete"+",操作参数为:"+selection);
        s = "";
        return 0;
    }

    @Override
    public int update(Uri uri,ContentValues values,String selection,String[] selectionArgs) {
        Log.v("test","update"+",操作参数为:"+selection);
        s = "update";
        return 0;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private ContentResolver resolver;
    //与AndroidManifest文件中声明的authorities相同才能匹配
    private Uri uri = Uri.parse("content://com.alphathink.zdh");

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

        //ContentResolver
        resolver = getContentResolver();

    }

    public void insert(View v){
        ContentValues cv = new ContentValues();
        cv.put("name","panda");
        resolver.insert(uri,cv);
    }

    public void update(View v){
        ContentValues cv = new ContentValues();
        cv.put("name","I am a message");
        resolver.update(uri,cv,"name",null);
    }

    public void query(View v){
        resolver.query(uri,null,"name",null,null);
    }

    public void delete(View v){
        resolver.delete(uri,"name",null);
    }

}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.alphathink.myactivity">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:allowTaskReparenting="true"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <provider
            android:authorities="com.alphathink.zdh"
            android:name=".MyContentProvider"
            android:exported="true"/>
    </application>

</manifest>

界面布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.alphathink.myactivity.MainActivity">
   <Button
       android:onClick="query"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/button"
       android:text="query"/>

   <Button
       android:onClick="insert"
       android:id="@+id/button2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="insert" />

   <Button
       android:onClick="delete"
       android:id="@+id/button3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="delete" />

   <Button
       android:onClick="update"
       android:id="@+id/button4"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_weight="1"
       android:text="update" />

</LinearLayout>

启动后点击查询:(结果为空,还没有数据操作,onCreate方法创建后执行一次。)

04-28 10:20:25.255 3486-3486/com.alphathink.myactivity V/test: onCreate
04-28 10:20:32.165 3486-3486/com.alphathink.myactivity V/test: query,操作参数为:name结果为:

点击插入后点击查询按钮:(数据已存入并可查询到了。)

04-28 10:21:34.905 3486-3486/com.alphathink.myactivity V/test: insert,操作参数为:name=panda
04-28 10:21:36.134 3486-3486/com.alphathink.myactivity V/test: query,操作参数为:name结果为:name=panda

点击删除按钮后点击查询:(数据模拟清空操作)

04-28 10:22:45.290 3486-3486/com.alphathink.myactivity V/test: delete,操作参数为:name
04-28 10:22:46.080 3486-3486/com.alphathink.myactivity V/test: query,操作参数为:name结果为:

点击更新按钮后点击查询操作:(数据模拟更新操作并更新完成)

04-28 10:23:24.867 3486-3486/com.alphathink.myactivity V/test: update,操作参数为:name
04-28 10:23:25.529 3486-3486/com.alphathink.myactivity V/test: query,操作参数为:name结果为:update

整个操作是在同一个app下完成的,manifest文件中定义的时候有个export属性,定义为true后可在其他应用里操作本应用暴露的ContentProvider数据,结果是一样的。设置为false后为其他应用不可访问。


总结

ContentProvider就像一个小网站,而ContentResolver就像一个客户端,客户端需要根据url来访问网站,网站核对好url后操作后台数据改变。具体使用我们还可以操作系统里的ContentProvider,比如通讯录。
ContentProvider很好的提供了进程间通信,使得应用间通信更容易。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值