2014-08-28 57 views

回答

0

試試這個...這個代碼,你會得到名稱爲N號碼保存在您的聯繫人列表,數組列表中或陣列添加它們

public void getContacts(ContentResolver cr) { 
    Cursor phones = cr.query(
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, 
      null, null); 
    while (phones.moveToNext()) { 
     String name = phones 
       .getString(phones 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
     String phoneNumber = phones 
       .getString(phones 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

    } 
    phones.close();// close cursor 

} 

調用方法

getContacts(this.getContentResolver()); 
0

,正則可能會顯示他們在列表視圖

try { 

      String phoneNumber = null; 

      Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 

      String _ID = ContactsContract.Contacts._ID; 

      String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 

      String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 

      Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 

      String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 

      String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 

      ContentResolver contentResolver = getContentResolver(); 

      Cursor cursor = contentResolver.query(CONTENT_URI, null, null, 
        null, null); 

      if (cursor.getCount() > 0) { 

       while (cursor.moveToNext()) { 

        String contact_id = cursor.getString(cursor 
          .getColumnIndex(_ID)); 

        int hasPhoneNumber = Integer 
          .parseInt(cursor.getString(cursor 
            .getColumnIndex(HAS_PHONE_NUMBER))); 

        if (hasPhoneNumber > 0) { 

         Cursor phoneCursor = contentResolver.query(
           PhoneCONTENT_URI, null, Phone_CONTACT_ID 
             + " = ?", new String[] { contact_id }, 
           null); 

         while (phoneCursor.moveToNext()) { 

          phoneNumber = phoneCursor.getString(phoneCursor 
            .getColumnIndex(NUMBER)); 



           String contname = phoneCursor.getString(phoneCursor 
             .getColumnIndex(DISPLAY_NAME)); 

           if (!contname.equals(null)) { 


            Toast.makeText(getApplicationContext(), 
              contname, Toast.LENGTH_SHORT).show(); 

           } 

          } 

         } 
         phoneCursor.close(); 

        } 

       } 

      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
相關問題