很多人在做手机联系人的apk时会遇到,如何获取手机联系人数据库的问题,在这里我就简单的将代码写一下
package com.example.song.lx_day14_contacts;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private String uri_raw = "content://com.android.contacts/raw_contacts";
//直接获取data表中的 数据类型是phone
private String uri_phone = "content://com.android.contacts/data/phones";
//直接获取data表中数据类型是emali
private String uri_emalis = "content://com.android.contacts/data/emails";
private String uri_data = "content://com.android.contacts/data";
private ListView listView;
private TextView empty;
private SimpleAdapter adapter;
private List<Map<String, String>> data = new ArrayList<>();
private ContentResolver contentResolver;//用来访问数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = ((ListView) findViewById(R.id.lv));
empty = ((TextView) findViewById(R.id.empty));
listView.setEmptyView(empty);//设置空视图
adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"_id", "display_Name", "data1"}, new int[]{R.id.id, R.id.name, R.id.number});
listView.setAdapter(adapter);
contentResolver = getContentResolver();
reloadListView();
}
private void reloadListView() {
data.clear();
data.addAll(selectData());
adapter.notifyDataSetChanged();
}
//查询工作 用于查询data raw_contact标 返回list数据 在刷新adapter
private List<Map<String, String>> selectData() {
Cursor cursor = contentResolver.query(Uri.parse(uri_raw), new String[]{"_id", "display_name"}, null, null, null);
//cursor返回的cursor数量不定
List<Map<String, String>> list = new ArrayList<>();
while (cursor != null && cursor.moveToNext()) {
String _id = cursor.getString(0);
String displayName = cursor.getString(1);
Map<String, String> map = new HashMap<>();
map.put("_id", _id);
map.put("display_Name", displayName);
//根据_id 去data查手机号
Cursor cursor1 = contentResolver.query(Uri.parse(uri_phone), new String[]{"data1"}, "raw_contact_id=?", new String[]{_id}, null);
//循环拿到手机号
StringBuffer sb = new StringBuffer();
//这里循环拿到手机号是因为有时联系人中 你可能一个人有几个电话
while (cursor1 != null && cursor1.moveToNext()) {
sb.append(cursor1.getString(0) + "\n");
}
cursor1.close();
map.put("data1", sb.toString());
list.add(map);
}
cursor.close();
return list;
}
}
在这里不要忘记了添加权限
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
您可能感兴趣的文章:Android获取手机通讯录、sim卡联系人及调用拨号界面方法Android 获取手机联系人实例代码详解android利用ContentResolver访问者获取手机联系人信息Android获取手机联系人信息Android获取手机联系人电话号码并返回结果Android读取手机通讯录联系人到自己项目Android编程实现读取手机联系人、拨号、发送短信及长按菜单操作方法实例小结浅谈Android手机联系人开发之增删查改功能Android ContentProvider获取手机联系人实例Android ContentProvider实现手机联系人读取和插入