《第一行代码Android》学习总结第七章 内容提供器ContentProvider

        主要用于在不同应用程序之间实现数据共享功能,它提供了一套完整的机制,允许一个程序访问另一个程序中的数据,同时还能保证数据的安全性。

ContentProvider一共有两种用法:

1、使用系统现有的ContentProvider来读取和操作相应程序中的数据。

2、创建自己的ContentProvider给程序的数据提供外部访问接口。

一、访问其他程序中的数据。

ContentResolver的基本用法

        对于一个应用程序来讲,如果想要访问内容提供器ContentProvider中的数据,就一定要借助ContentResolver类,该类实例可以通过Context的getContentResolver()方法获得。ContentResolver中提供了insert()、update()、delete()、query()等方法对数据进行CRUD操作。

        与SQLiteDatabase不同,ContentResolver中的增删改查并不是接受表名参数,而是使用一个Uri作为参数代替,成为内容URI。内容URI为内容提供器中的数据建立了唯一标识符。

content://com.example.app.provider/table1
content://com.example.app.provider/table2

格式:协议声明+authority+path

authority:用于对不同应用程序做区分,一般避免冲突都会采用程序报名的方式做命名。

Path:对于同一应用程序中不同的表做区分,加在authority之后。

        在得到内容URI字符串之后,还需要将它解析成Uri对象作为参数传入:

Uri uri = Uri.parse(“content://com.example.app.provider/table1”)

实例:读取系统联系人

1、将读取到的信息联系人在ListView中显示,修改activity_main.xml文件。

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView
        android:id="@+id/contacts_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </ListView>
</LinearLayout>

2、修改MainActivity中代码。

public class MainActivity extends AppCompatActivity {
    ArrayAdapter<String> adapter;
    List<String> contactsList = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//创建ListView并创建适配器
        ListView contactsView = (ListView) findViewById(R.id.contacts_view);
        adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,contactsList);
        contactsView.setAdapter(adapter);
//运行时权限
        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(this,new String[]{ Manifest.permission.READ_CONTACTS },1);
        }else {
            readContacts();
        }
}
//读取系统联系人信息
    private void readContacts() {
        Cursor cursor = null;
        try{
//查询联系人数据,其中ContactsContract.CommonDataKinds.Phone.CONTENT_URI已做好封装
            cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,null,null,null);
            if(cursor != null){
//遍历cursor
                while(cursor.moveToNext()){
                    String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    contactsList.add(displayName + "\n" + number);
                    adapter.notifyDataSetChanged();
                }
            }
        }catch (Exception e){
            e
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值