Android手机卫士之获取联系人信息显示与回显
作者:wuyudong 时间:2021-08-03 21:33:07
前面的文章已经实现相关的布局,本文接着进行相关的功能实现
读取系统联系人
当点击“选择联系人”按钮后,弹出联系人列表,读取系统联系人分如下几个步骤:
系统联系人提供了一个内容提供者,通过内容解析器,匹配Url地址
1.内容解析器
2.Url地址,查看系统联系人数据库,内容提供者源码
先看api文档的清单文件,后看java类(联系人数据库有多张表)
contents://com.android.contacts/表名
3.系统联系人数据库中核心表的表结构
raw_contacts 联系人表: contact_id 联系人唯一性id值
data 用户信息表:raw_contact_id作为外键,和raw_contacts中contact_id做关联查询
获取data1字段,包含了电话号码以及联系人名称
mimetype_id字段,包含了当前行data1对应的数据类型
mimetypes 类型表: 获取data表中mimetype_id和mimetypes中_id做关联查询,获取指向的信息类型
电话号码:vnd.android.cursor.item/phone_v2
用户名称:vnd.android.cursor.item/name
4.表的访问方式
content://com.android.contacts/raw_contacts
content://com.android.contacts/data
下面用代码实现
private ListView lv_contact;
private List<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
private MyAdapter mAdapter;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
//8,填充数据适配器
mAdapter = new MyAdapter();
lv_contact.setAdapter(mAdapter);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_list);
initUI();
initData();
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return contactList.size();
}
@Override
public HashMap<String, String> getItem(int i) {
return contactList.get(i);
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View v = View.inflate(getApplicationContext(), R.layout.listview_contact_item, null);
TextView tv_name = (TextView)v.findViewById(R.id.tv_name);
TextView tv_phone = (TextView)v.findViewById(R.id.tv_phone);
tv_name.setText(getItem(i).get("name"));
tv_phone.setText(getItem(i).get("phone"));
return v;
}
}
/**
* 获取联系人数据的方法
*/
private void initData() {
//因为读取系统联系人,可能是一个耗时操作,放置到子线程中处理
new Thread(){
public void run(){
//1,获取内容解析器对象
ContentResolver contentResolver = getContentResolver();
//2,做查询系统联系人数据库表过程(读取联系人权限)
Cursor cursor = contentResolver.query(
Uri.parse("content://com.android.contacts/raw_contacts"),
new String[]{"contact_id"},
null, null, null);
contactList.clear();
//3,循环游标,直到没有数据为止
while (cursor.moveToNext()){
String id = cursor.getString(0);
//4,根据用户唯一性id值,查询data表和mimetype表生成的视图,获取data以及mimetype字段
Cursor indexCursor = contentResolver.query(
Uri.parse("content://com.android.contacts/data"),
new String[]{"data1","mimetype"},
"raw_contact_id = ?", new String[]{id}, null);
//5,循环获取每一个联系人的电话号码以及姓名,数据类型
HashMap<String, String> hashMap = new HashMap<String, String>();
while (indexCursor.moveToNext()){
String data = indexCursor.getString(0);
String type = indexCursor.getString(1);
//6,区分类型去给hashMap填充数据
if(type.equals("vnd.android.cursor.item/phone_v2")) {
//数据非空判断
if(!TextUtils.isEmpty(data)) {
hashMap.put("phone", data);
}
}else if(type.equals("vnd.android.cursor.item/name")) {
if(!TextUtils.isEmpty(data)) {
hashMap.put("name", data);
}
}
}
indexCursor.close();
contactList.add(hashMap);
}
cursor.close();
//7,消息机制,发送一个空的消息,告知主线程可以去使用子线程已经填充好的数据集合
mHandler.sendEmptyMessage(0);
}
}.start();
}
实现的效果如下:
联系人信息回显
接下来实现点击联系人条目,实现回显,例如双击第一个条目,号码自动添加
代码如下:
private void initUI() {
lv_contact = (ListView) findViewById(R.id.lv_contact);
lv_contact.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//1,获取点中条目的索引指向集合中的对象
if(mAdapter != null) {
HashMap<String, String> hashMap = mAdapter.getItem(i);
//2,获取当前条目指向集合对应的电话号码
String phone = hashMap.get("phone");
//3,此电话号码需要给第三个导航界面使用
//4,在结束此界面回到前一个导航界面的时候,需要将数据返回过去
Intent intent = new Intent();
intent.putExtra("phone", phone);
setResult(0, intent);
finish();
}
}
});
}
接着onActivityResult中添加下面的代码
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(data != null) {
//1,返回到当前界面的时候,接受结果的方法
String phone = data.getStringExtra("phone");
//2,将特殊字符过滤(中划线转换成空字符串)
phone = phone.replace("-", "").replace(" ", "").trim();
et_phone_number.setText(phone);
//3,存储联系人至sp中
SpUtil.putString(getApplicationContext(), ConstantValue.CONTACT_PHONE, phone);
}
super.onActivityResult(requestCode, resultCode, data);
}
当填写号码后,进入下一页,再次返回,发现号码不见了,于是使用sp存储并从中读取
private void initUI() {
//显示电话号码的输入框
et_phone_number = (EditText)findViewById(R.id.et_phone_number);
//获取联系人电话号码回显过程
String contact_phone = SpUtil.getString(this, ConstantValue.CONTACT_PHONE, "");
et_phone_number.setText(contact_phone);
bt_select_number = (Button) findViewById(R.id.bt_select_number);
//点击选择联系人的对话框
bt_select_number.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), ContactListActivity.class);
startActivityForResult(intent, 0);
}
});
}
来源:http://www.cnblogs.com/wuyudong/archive/2016/10/12/5951794.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MyBatisPlus3.x中使用代码生成器(全注释)
![](https://img.aspxhome.com/file/2023/6/125656_0s.jpg)
Mybatis 如何开启控制台打印sql语句
![](https://img.aspxhome.com/file/2023/5/99865_0s.jpg)
Windows下安装ElasticSearch的方法(图文)
![](https://img.aspxhome.com/file/2023/2/60192_0s.png)
java实例方法被覆盖,静态方法被隐藏Explain(详解)
C# HttpClient 如何使用 Consul 发现服务
![](https://img.aspxhome.com/file/2023/8/111198_0s.png)
Spring-Data-JPA整合MySQL和配置的方法
![](https://img.aspxhome.com/file/2023/4/58734_0s.png)
java实现Yaml转Json示例详解
java 字浮串提取方法汇集
使用JSONObject生成和解析json的方法
vista和win7在windows服务中交互桌面权限问题解决方法:穿透Session 0 隔离
![](https://img.aspxhome.com/file/2023/0/72480_0s.png)
Spring深入探索AOP切面编程
![](https://img.aspxhome.com/file/2023/6/78056_0s.png)
Java 随机生成验证码(支持大小写字母、数字、随机字体)的实例
如何设置Spring Boot测试时的日志级别
spring mvc中的@ModelAttribute注解示例介绍
浅谈Android应用安全防护和逆向分析之apk反编译
![](https://img.aspxhome.com/file/2023/7/91677_0s.jpg)
SpringMVC 限流的示例代码
Android实现CoverFlow效果控件的实例代码
![](https://img.aspxhome.com/file/2023/5/83545_0s.png)
浅谈Maven镜像更换为阿里云中央仓库(精)
一文带你入门SpringMVC的配置与使用
![](https://img.aspxhome.com/file/2023/7/62137_0s.png)
Java通过jersey实现客户端图片上传示例
![](https://img.aspxhome.com/file/2023/4/86804_0s.jpg)