当前位置:网站首页>四大组件---ContentResolver
四大组件---ContentResolver
2022-08-11 02:03:00 【高朵】
四大组件---ContentResolver
内容提供器的用法一般有两种:
(1) 使用现有的内容提供器来读取和操作相应程序的数据
(2) 创建自己的内容提供器给我们程序的数据提供外部接口。
一、 ContentResolver的基本用法
ContentResolver是通过URI来查询ContentProvider中提供的数据。
内容URI标准写法如下:
content://com.example.app.provider/table1
内容URI字符串能清楚的表达出是哪个程序的哪个表。ContentResolver中的增删改查方法接收内容URI。需要把URI字符串解析成URI对象。调用Uri.parse()方法即可解析。
Uri uri = Uri.parse("content://com.example.app.provider/table1")
用URI对象查询表table1表中数据
Cursor cursor = getContentResolver().query(
uri,
projection,
selection,
selectionArgs,
sortOrder);

查询完成后返回的仍然是Cursor对象,将数据从crsor对象中逐个读取
if(cursor != null){
while(cursor.moveToNext()){
String column1 = cursor.getString(cursor.getColunmIndex("colunm1"));
int column2 = cursor.getInt(cursor.getColumnIndex("column2");
}
cursor.close();
}
添加
ContentValues values = new ContentValues();
values.put("column1", "text");
valuse.put("column2", 1);
getContentResolver().insert(uri.values);
更新
ContentValues values = new ContentValues();
values.put("column1", "");
getContentResolver().update(uri, values, "column1 = ? and column2 = ?", new String[]{
"text","1"});
删除
getContentResolver().delete(uri, "column2 = ?", new String[]{
"1"});
二、 读取系统联系人
首先先创建三个联系人

希望最终读取出的联系人信息能在ListView中显示出来。修改主活动xml文件
新建一个列表
修改主活动代码
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 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 {
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (cursor != null) {
while (cursor != null) {
//获取联系人姓名
@SuppressLint("Range") String displayName = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
@SuppressLint("Range") String number = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactsList.add(displayName + "\n" + number);
}
adapter.notifyDataSetChanged();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
readContacts();
} else {
Toast.makeText(this, "u denied the permission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
}
在AndriodMainfest.xml修改代码
<uses-permission android:name="android.permission.READ_CONTACTS"/>

点击ALLOW,联系人的数据都成功读取出来了,说明跨程序访问数据的功能确实是实现了。

边栏推荐
猜你喜欢

微信公众号后台管理
![划分字母区间[贪心->空间换时间->数组hash优化]](/img/bb/e750c7cd4a80e767bd64d96ffc2ce3.png)
划分字母区间[贪心->空间换时间->数组hash优化]

Alibaba 最新神作!耗时 182 天肝出来 1015 页分布式全栈手册太香了

经典面试题 之 GC垃圾收集器

ARM development (4) How to read the chip manual for novice Xiaobai, bare metal driver development steps and pure assembly to achieve lighting, assembly combined with c lighting, c to achieve lighting

Deep Learning【第二章】

MySQL - an SQL in MySQL is how to be performed?

This Thursday evening at 19:00, Lesson 5 of the sixth phase of knowledge empowerment丨OpenHarmony WiFi subsystem

请讲一讲JS中的 for...in 与 for...of (下)

Lianshengde W801 series 6-Analyze the Bluetooth communication source code of W801 from the perspective of WeChat applet (indicate method)
随机推荐
两日总结九
Ora - 00001 in violation of the only constraint
SyntaxError: invalid syntax
2022英伟达显卡排名天梯图
The latest domestic power supply manufacturers and pin-to-pin replacement manuals for specific models are released
sql 使用到where和groupby时到底怎么建立索引?
如何解决高度塌陷
软件测试面试题:软件测试的过程的V模型,说出它的缺点?
【iframe父页面调用子页面的方法】踩坑:获取元素的时候需要用 `[x]`是关键,不能用`.eq(x)`否则获取不到。
数据存储全方案----详解持久化技术
Please talk about for...in and for...of in JS (below)
数据库数据采集利器FlinkCDC
请讲一讲JS中的 for...in 与 for...of (下)
阿里的数据同步神器——Canal
本周四晚19:00知识赋能第六期第5课丨OpenHarmony WiFi子系统
数论基础-整除(编程例题)
软件测试面试题:性能测试工作?
单面PCB布线阻抗的工程设计
【PHP】入门知识
[The method of calling the child page from the parent page of the iframe] Stepping on the pit: It is the key to use `[x]` when getting elements. You cannot use `.eq(x)`, otherwise it will not be obtai