获取手机通讯录信息示例

非常简单的的一个Demo示例直接上Java代码如下:

public class MainActivity extends Activity {
   
   private String uri_raw = "content://com.android.contacts/raw_contacts";
   private String uri_phone = "content://com.android.contacts/data/phones";   
   private ListView listView  = null;
   private TextView empty    = null;
   private ContentResolver contentResolver; 
   private List<Map<String, String>> data = new ArrayList<Map<String,String>>();
   private SimpleAdapter adapter;
   
   @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(); //赋值  不要导v4
      reloadListView();
   }
   private void reloadListView() {
      // TODO Auto-generated method stub
      data.clear(); //原来的数据清除
      data.addAll(selectData()); 
      adapter.notifyDataSetChanged();
   }
   //查询工作  用于查询Data  raw_contact表 返回List数据 在刷新Adapter
   private List<Map<String, String>> selectData() {
      //1.先查询 raw 获取_id 查出所有联系人.
      //2.循环查询--> 根据_id data表中(_id = raw_contacts_id)查询 data1数据
      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<Map<String,String>>();     
      while (cursor!= null && cursor.moveToNext()) {       
         String _id = cursor.getString(0);
         String dispalyName  = cursor.getString(1);
         Map<String, String> map = new HashMap<String, String>();
         map.put("_id", _id);
         map.put("display_Name", dispalyName);        
         //根据_id data查手机号
         Cursor cursor2 = contentResolver.query(Uri.parse(uri_phone), new String[]{"data1"}, 
               "raw_contact_id=?", new String []{_id}, null);
         //循环拿到手机号
         StringBuffer sb = new StringBuffer();
         while (cursor2!=null && cursor2.moveToNext()) {           
            sb.append(cursor2.getString(0)+"\n");           
         }
         cursor2.close(); //关闭游标
         map.put("data1", sb.toString());
         list.add(map);
      }    
      cursor.close();
      return list;
   }
   
   
   
}

安卓开发获取手机通讯录信息可以通过ContentResolver和Cursor实现。具体步骤如下: 1.在AndroidManifest.xml文件中添加获取通讯录权限 2.创建ContentResolver对象 3.创建Cursor对象,使用ContentResolver的query()方法查询通讯录数据 4.遍历Cursor对象,获取通讯录数据 下面是一个示例代码: ``` if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { //permission not granted, request for permission ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, PERMISSION_READ_CONTACTS); }else { //permission granted, read contacts readContacts(); } private void readContacts() { ContentResolver contentResolver = getContentResolver(); Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); if (cursor != null && cursor.moveToFirst()) { do { //获取联系人姓名 String name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); //获取联系人电话号码 Cursor phoneCursor = contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + cursor.getString( cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID)), null, null); if (phoneCursor != null && phoneCursor.moveToFirst()) { do { String phone = phoneCursor.getString(phoneCursor.getColumnIndex( ContactsContract.CommonDataKinds.Phone.NUMBER)); //处理联系人姓名和电话号码数据 } while (phoneCursor.moveToNext()); phoneCursor.close(); } } while (cursor.moveToNext()); cursor.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值