【问题标题】:How to access contacts如何访问联系人
【发布时间】:2019-02-22 07:13:20
【问题描述】:

挑选联系人类似于打开图库并挑选图片,但我如何访问所有联系人并将其与我的应用相关联。 为了练习,我正在尝试创建一个基本的聊天应用程序,因此我不希望注册的用户显示在我的活动中,但我只想显示我的联系人中的用户以及使用我的应用程序的用户,就像 whatsapp 、微信或任何其他短信应用程序。我也在想我是否可以像联系人一样访问收藏夹......我怎样才能做到这一点?

【问题讨论】:

    标签: java android xml android-studio contacts


    【解决方案1】:

    要检索所有联系人,您可以使用以下内容:

        final ArrayList<Contact> contacts = new ArrayList<>();
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        if (cursor != null) {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                    if (hasPhoneNumber > 0) {
                        String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                        String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                        Cursor phoneCursor = contentResolver.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id},
                                null);
                        if (phoneCursor != null) {
                            if (phoneCursor.moveToNext()) {
                                String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    
                                contacts.add(new Contact(name, phoneNumber));
                                phoneCursor.close();
                            }
                        }
                    }
                }
            }
    
            cursor.close();
        }
    

    为了获取唯一使用聊天的人,也许您应该将该信息保存在服务器中,例如使用电话号码作为 id。当您检索所有联系人时,您可以比较电话号码并仅将在您的服务器中注册的那些添加到数组中......

    【讨论】:

      【解决方案2】:
          ContentResolver cr = context.getApplicationContext().getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,
                  null, null, null, null);
      
          if (cur.getCount() > 0) {
              while (cur.moveToNext()) {
      
                  String id;
                  String name;
                  String phoneNumbers = "";
      
                  id = cur.getString(
                          cur.getColumnIndex(ContactsContract.Contacts._ID));
                  name = cur.getString(cur.getColumnIndex(
                          ContactsContract.Contacts.DISPLAY_NAME));
      
                  if (cur.getInt(cur.getColumnIndex(
                          ContactsContract.Contacts.HAS_PHONE_NUMBER)) > 0) {
                      Cursor pCur = cr.query(
                              ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                              null,
                              ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                              new String[]{id}, null);
                      while (pCur.moveToNext()) {
                          String pn = pCur.getString(pCur.getColumnIndex(
                                  ContactsContract.CommonDataKinds.Phone.NUMBER));
      
                          if (pCur.isFirst())
                              phoneNumbers += pn;
                          else
                              phoneNumbers += ", " + pn;
      
      
                      }
                      pCur.close();
                  }
              }
          }
      
          cur.close();
      

      【讨论】:

        猜你喜欢
        • 2014-02-09
        • 2011-07-11
        • 1970-01-01
        • 1970-01-01
        • 2014-06-22
        • 1970-01-01
        • 2011-11-04
        • 2012-04-20
        • 1970-01-01
        相关资源
        最近更新 更多