您的位置:首页 > 其它

内容提供者获取手机联系人

2017-02-21 21:34 399 查看


           今天给大家讲讲安卓四大基本组件中用内容接收者实现获取手机联系人,讲两种方式,一是底层的做法获取手机联系人,二是跳到系统联系人页面拿手机联系人
    现在先看两张效果图,因涉及了一些私人的号码,所以小编就涂鸦啦。

         




     如图所看,第一张图的效果是用底层做法获取联系人号码在遍历在自己的页面,现在我们就来上代码

//得到访问者
cr = getContentResolver();
//实例化一个的list集合的数据
listdata = new ArrayList<Map<String,Object>>();
//设置适配器
simpleAdapter = new SimpleAdapter(this, listdata,android.R.layout.simple_list_item_2,new String[]{"names","phones"},new int[]{android.R.id.text1,android.R.id.text2});
lv_id_listview.setAdapter(simpleAdapter);
  //获取联系人号码(底层做法)
public void getContacts(View view){
Cursor cursor=cr.query(Uri.parse("content://com.android.contacts/raw_contacts"),null,null,null,null);
while(cursor.moveToNext()){
Map<String,Object> map=new HashMap<String,Object>();
int uid=cursor.getInt(cursor.getColumnIndex("_id"));
String uname=cursor.getString(cursor.getColumnIndex("display_name"));
Log.i("test",uid+" "+uname);
map.put("names",uname);
Cursor cursor2=cr.query(Uri.parse("content://com.android.contacts/raw_contacts/"+uid+"/data"),null,null,null,null);
while(cursor2.moveToNext()){
String type= cursor2.getString(cursor2.getColumnIndex("mimetype"));
if ("vnd.android.cursor.item/phone_v2".equals(type)){
data1 = cursor2.getString(cursor2.getColumnIndex("data1"));
Log.i("test",type+" "+data1);
map.put("phones",data1);
Notity(data1,map);//(大循环的里面,小循环的外面,就是循环两个循环数据里面的数据。放到小循环里就只显示有号码的联系人)
}
}
}
//通知适配器发生改变
simpleAdapter.notifyDataSetChanged();
}
这就是第一种底层的做法
现在来看第二种直接获取系统的联系人

跳转到手机联系人的界面

//获取系统联系人号码
public void getSystem(View view){
Intent intentphone = new Intent(Intent.AC
4000
TION_PICK);
intentphone.setData(ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intentphone, 0);
}点击某个联系人获取选中的信息,就是要带结果返回,所以要用Activity中带结果返回的onActivityResult方法,看代码
/带结果返回
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case RESULT_OK:
Map<String,Object> map=new HashMap<String,Object>();
ContentResolver contentResolver = getContentResolver();//ContentResolver实例带的方法可以实现找到指定的ContentProvider并获取到他的数据
Uri contactDate = data.getData();//uri,每个内容提供者定义一个唯一的公开的uri,用于指定他的数据集,查询就是输入uri等参数,其中uri是必须,其他的是可选,如果系统能找到uri对应的内容提供者将返回一个cursor对象
Cursor cursor = managedQuery(contactDate, null, null, null, null);
cursor.moveToFirst();
//获取data表中的名字
String uanme = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String nameId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));//条件为联系人的id
//获取data表中的电话号码,条件为联系人id,因为手机号码可能为多个
Cursor phone = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + nameId, null, null);
while (phone.moveToNext()) {
String phonenumber = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
map.put("phones",phonenumber);
map.put("names",uanme);
Notity(phonenumber,map);//自定义的通知适配器改变
}
break;
default:
break;
}
}

此方法通知适配器改变需要把通知写到外面,在到里边调用,适配器才能发生改变,否则不会有作用。因为小编是把两者的一起点击用的,这样的话,就会有一个问题,就是在点击第二个获取系统的号码后,在点击第一个获取号码便会添加两个相同的号码,所以为了解决这个问题,便是在自定义的通知适配器改变的方法里,做去重复的操作,看代码
//通知适配器发生改变
public void Notity(String phonenumber,Map<String, Object> map){
if (!listdata.isEmpty()){
for (Map<String, Object> stringObjectMap : listdata) {
String phones= (String) stringObjectMap.get("phones");
dataphone.add(phones);
}
if (dataphone.contains(phonenumber)){
Toast.makeText(this, phonenumber+"已经添加过", Toast.LENGTH_SHORT).show();
}else{
listdata.add(map);
}
}else {
listdata.add(map);
}
simpleAdapter.notifyDataSetChanged();
}这两种方式就完成了,最后要记住的是要记得加权限:
读取手机联系人的权限

<uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: