2016-08-09 12 views
1

連絡先の名前と番号を取得するためのコードは次のとおりです。携帯電話番号と連絡先の名前のみを取得するにはどうすればよいですか?連絡先の名前にはいくつかの番号が付いている場合があります。名前の携帯電話番号を取得するには?連絡先に電話番号のみを取得する方法

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

電話番号が有効かどうかを確認するには、https://github.com/googlei18n/libphonnumberこのライブラリを使用します。 –

答えて

0

このコードを使用してください。私のために働く。

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

あなたは数のタイプを決定するために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)); 
} 
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(); 
      } 

     } 
    } 

あなたはこの許可を求める必要があります

uses-許可アンドロイド:名前= "android.permission.READ_CONTACTS"

関連する問題