【问题标题】:How get only mobile numbers in contact如何只获取联系人的手机号码
【发布时间】:2016-12-15 10:07:04
【问题描述】:

我有以下代码来获取联系人姓名和电话号码。如何只获取联系人中的手机号码和姓名?联系人中的姓名可能有几个数字。如何获取姓名的手机号码?

 ContentResolver cr = activity.getContentResolver();
 Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
 while (phones.moveToNext()) {
    name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
    phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
 }

【问题讨论】:

标签: java android android-contacts phone-number


【解决方案1】:

尝试使用此代码。为我工作。

private static final String[] PROJECTION =
        {
                Contacts._ID,
                Contacts.LOOKUP_KEY,
                Contacts.HAS_PHONE_NUMBER,
                Build.VERSION.SDK_INT
                        >= Build.VERSION_CODES.HONEYCOMB ?
                        Contacts.DISPLAY_NAME_PRIMARY :
                        Contacts.DISPLAY_NAME

        };
private static final String[] PROJECTION_PHONE =
        {
                Phone.NUMBER
        };
private static final String SELECTION_PHONE = Phone.LOOKUP_KEY + " = ?";

HashMap<String, ArrayList<String>> contacts = new HashMap<>();

    ContentResolver cr = context.getContentResolver();
    Cursor cur = cr.query(Contacts.CONTENT_URI,
            PROJECTION, null, null, null); //get contacts
    if (cur.getCount() > 0) {
        while (cur.moveToNext()) {
            String id = cur.getString(
                    cur.getColumnIndex(Contacts._ID));
            String name = cur.getString(
                    cur.getColumnIndex(Build.VERSION.SDK_INT
                            >= Build.VERSION_CODES.HONEYCOMB ?
                            Contacts.DISPLAY_NAME_PRIMARY :
                            Contacts.DISPLAY_NAME));
            String lookUpKey = cur.getString(cur.getColumnIndex(Contacts.LOOKUP_KEY));
            if (Integer.parseInt(cur.getString(cur.getColumnIndex(Contacts.HAS_PHONE_NUMBER))) > 0) { //check if has numbers
                Cursor pCur = cr.query(Phone.CONTENT_URI, PROJECTION_PHONE, SELECTION_PHONE,
                        new String[]{lookUpKey}, null); //get contacts phone numbers
                ArrayList<String> phones = new ArrayList<>();
                while (pCur.moveToNext()) {
                    String phone = pCur.getString(0);
                    phones.add(phone);
                }
                contacts.put(name, phones);
                pCur.close();
            }
        }
    }
    cur.close();

【讨论】:

    【解决方案2】:

    您可以查询ContactsContract.CommonDataKinds.Phone.TYPE 列来确定号码的类型:

    int type = phones.getInt(phones.
        getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
    
    if(type == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
        phoneNumber = phones.getString(phones.
            getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    }
    

    【讨论】:

      【解决方案3】:
       ContentResolver cr = getContentResolver();
          Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                  null, null, null);
          if (cur.getCount() > 0) {
              while (cur.moveToNext()) {
                  String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                             if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) 
                  {
                      // Query phone here. Covered next
                      Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,null, null); 
                      while (phones.moveToNext()) { 
                               String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                               Log.i("Number", phoneNumber);
                              } 
                      phones.close(); 
                  }
      
              }
          }
      

      您必须申请此权限:

      使用权限 android:name="android.permission.READ_CONTACTS"

      【讨论】:

        猜你喜欢
        • 2011-10-30
        • 1970-01-01
        • 2020-07-13
        • 2012-08-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多