Android ContentProvider获取手机联系人实例

Android ContentProvider获取手机联系人实例,第1张

概述在做项目的时候,因为要用到我们自动获取联系人的姓名和电话,就想到了ContentProvider分享数据的功能,这样做既节省了时间,也减少了我们输入错误号码的几率,所以,想在这里把小demo分享给大家,方便以后要用的时候

在做项目的时候,因为要用到我们自动获取联系人的姓名和电话,就想到了ContentProvIDer分享数据的功能,这样做既节省了时间,也减少了我们输入错误号码的几率,所以,想在这里把小demo分享给大家,方便以后要用的时候可以看看

我们先看下获取所有联系人的方式,把所有联系人展示在ListVIEw上

public voID getlinkMan(VIEw vIEw){    //获取联系人    Uri uri=Uri.parse("content://com.androID.contacts/raw_contacts");    cursor = cr.query(uri,null,null);    while(cursor.movetoNext()){      int ID= cursor.getInt(cursor.getColumnIndex("_ID"));      String name= cursor.getString(cursor.getColumnIndex("display_name"));      int number=cursor.getInt(cursor.getColumnIndex("number"));      Log.i("test",ID+" "+name);      //继续获取相对应的联系人的数据(电话号码)      Uri urIData=Uri.parse("content://com.androID.contacts/raw_contacts/"+ID+"/data");      cursorData = cr.query(urIData,null);      while(cursorData.movetoNext()){        String data1= cursorData.getString(cursorData.getColumnIndex("data1"));        String type= cursorData.getString(cursorData.getColumnIndex("mimetype"));        if("vnd.androID.cursor.item/phone_v2".equals(type)){          Log.i("test","    "+data1+":"+type);          Map<String,Object> map=new HashMap<>();          map.put("ID",ID);          map.put("name",name);          map.put("number",number);          List.add(map);        }      }    }    //实例化适配器    simpleAdapter = new SimpleAdapter(this,List,R.layout.item_linkman,new String[]{"_ID","name","number"},new int[]{R.ID.tv_item_List_ID,R.ID.tv_item_List_name,R.ID.tv_item_List_number});    ListVIEw.setAdapter(simpleAdapter);  }

在控制台上输出的效果如图

在模拟器上的效果就是一个ListVIEw

然后我们着重来看一下获取手机联系人

1)跳转到系统联系人界面
2)选择一个联系人
3)获取联系人名、手机号码
4)回调显示名字和号码

1)、跳转到系统联系人的界面代码可以在Intent跳转大全里面找

//跳转到通讯录界面    Uri uri=Uri.parse("content://contacts/people");    Intent intent=new Intent(Intent.ACTION_PICK,uri);    startActivity(intent);

2)、选择联系人,回调过去

@OverrIDe   protected voID onActivityResult(int requestCode,int resultCode,Intent data) {     switch (requestCode){       case 0:         if(data==null) //判断返回的数据        {           return;         }         //获取选择的联系人信息         Uri uri=data.getData();         String contact[]=getPhoneContacts(uri);         Map<String,Object> map = new HashMap<String,Object>();         map.put("name",contact[0]);         map.put("number",contact[1]);         List.clear();         List.add(map);         adapter = new SimpleAdapter(this,R.layout.item_List,new String[]{"ID",new int[]{R.ID.ID,R.ID.name,R.ID.number});         ListVIEw.setAdapter(adapter);         break;     }     super.onActivityResult(requestCode,resultCode,data);   } 

3)、根据选择人的ID,去数据库里面的表查询数据,返回联系人的姓名和号码,但是需要注意的是,我们的联系人和和联系人的手机号并不在一个表里,所以这时候我们需要对数据进行一些处理

private String[] getPhoneContacts(Uri uri){    String[] contact=new String[2];    ContentResolver cr = getContentResolver();    //取得联系人中第一项的光标    Cursor cursor=cr.query(uri,null);    if(cursor!=null)    {      cursor.movetoFirst();      //取得联系人姓名      int nameFIEldColumnIndex=cursor.getColumnIndex(ContactsContract.Contacts.disPLAY_name);      contact[0]=cursor.getString(nameFIEldColumnIndex);      //取得电话号码      String ContactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));      Cursor phone = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + ContactID,null);      if(phone != null){        phone.movetoFirst();        contact[1] = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));      }      phone.close();      cursor.close();    }    else    {      return null;    }    return contact;  }

4)、然后添加权限,再把项目运行一遍,就可以看到我们的效果图了

<uses-permission androID:name="androID.permission.READ_CONTACTS" /><uses-permission androID:name="androID.permission.READ_PHONE_STATE" />

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android ContentProvider获取手机联系人实例全部内容,希望文章能够帮你解决Android ContentProvider获取手机联系人实例所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147007.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存