2016-08-09 6 views
1

Ich habe den folgenden Code, um Kontakt Name und Nummer zu erhalten. Wie bekomme ich nur Handy-Nummern und Namen in Kontakt? Ein Name in Kontakt kann ein paar Nummern haben. Wie bekomme ich Handy-Nummern für einen Namen?Wie bekomme ich nur Handy-Nummern in Kontakt

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)); 
} 
+0

Verwenden Sie diese Bibliothek https://github.com/googlei18n/libphonummer, um zu überprüfen, ob die Telefonnummer gültig ist oder nicht. –

Antwort

0

Versuchen Sie, diesen Code zu verwenden. Arbeite für mich.

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(); 
0

Sie konnten die ContactsContract.CommonDataKinds.Phone.TYPE Spalte abfragen, um die Art der Zahl zu bestimmen:

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)); 
} 
0
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(); 
      } 

     } 
    } 

Sie müssen für diese Erlaubnis fragen:

Gebrauch- Erlaubnis android: name = "android.permission.READ_CONTACTS"

Verwandte Themen