Android 获取所有的联系人数据 工具类

本文详细介绍了如何在Android中创建一个工具类,用于获取设备上的所有联系人数据。通过使用ContentResolver和ContactsContract类,我们可以查询到姓名、电话号码、邮箱等信息。这个工具类对于需要处理联系人信息的Android应用开发者来说非常实用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  /**
    * 获取所有的联系人数据 <uses-permission
    * android:name="android.permission.READ_CONTACTS" />
    *
    * @return
    */
   public static ArrayList<ContactsInfo> getAllContacts(Context context) {
      ArrayList<ContactsInfo> infos = new ArrayList<ContactsInfo>();// 所有的数据
      // 获取内容解析者
      ContentResolver contentResolver = context.getContentResolver();
      // Uri uri = Uri.parse("content://com.android.contacts/raw_contacts");
      Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
      // 要查询的字段
      String[] projection = new String[] {
            ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,// 名字
            ContactsContract.CommonDataKinds.Phone.NUMBER, // 电话
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID };// 联系人id


      // 参1 查询的uri 参2 要查询的字段 参3 查询的条件 参4 条件里?对应的值 参5 排序
      Cursor cursor = contentResolver
            .query(uri, projection, null, null, null);
      if (cursor != null) {
         while (cursor.moveToNext()) {
            String name = cursor.getString(0);
            String number = cursor.getString(1);
            int contactsId = cursor.getInt(2);
            ContactsInfo info = new ContactsInfo(name, number, contactsId);
            infos.add(info);
         }
         cursor.close();
      }
      return infos;
   }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值