2014-02-26 3 views
0

Android 기기의 전화 번호부에서 모든 연락처를 가져 오는 응용 프로그램을 만듭니다. Gmail에서 내 연락처 목록을 Android 기기와 동기화하면 모든 연락처가 전화 번호부에 표시됩니다. 그러나 응용 프로그램을 실행할 때 전화 번호부에서 연락처를 가져올 수 없습니다.전화 번호부에서 연락처를 가져올 수 없음 android

여기 MainActivity.java

public class MainActivity extends Activity { 
    private TextView outputText; 
    TextView txtViewContactsInfor; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     outputText =(TextView)findViewById(R.id.textView2); 
     txtViewContactsInfor = (TextView) findViewById(R.id.txtViewContactsInfor); 
     Import_contacts_from_address_book(); 
    } 


    private void Import_contacts_from_address_book() { 
     // TODO Auto-generated method stub 
     String phoneNumber = null; 
     String email = 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 PHONECONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
     String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 

     Uri EMAILCONTENT_URI = ContactsContract.CommonDataKinds.Email.CONTENT_URI; 
     String EMAILCONTACT_ID = ContactsContract.CommonDataKinds.Email.CONTACT_ID; 
     String EMAIL = ContactsContract.CommonDataKinds.Email.DATA; 

     StringBuffer output = new StringBuffer(); 
     ContentResolver contentResolver = getContentResolver(); 
     Cursor cursor = contentResolver.query(CONTENT_URI, null, null, null, null); 
     System.out.println("---------------------->"+cursor.getCount()); 
     if(cursor.getCount() >0){ 
      int aa = cursor.getCount(); 
      while(cursor.moveToNext()){ 
       String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 
       String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); 
       int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 

       if(hasPhoneNumber > 0){ 
        output.append("\nFirst Name: "+name); 
        // Query and loop for every phone number of the contact 
        Cursor phoneCursor = contentResolver.query(PHONECONTENT_URI, null, PHONECONTACT_ID + "=?", new String[]{contact_id}, null); 
        while(phoneCursor.moveToNext()){ 
         phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
         output.append("\n Phone number: "+phoneNumber); 
        } 
        phoneCursor.close(); 
        // Query and loop for every email of the contact 

        Cursor emailCurosr = contentResolver.query(EMAILCONTENT_URI, null, EMAILCONTACT_ID+"=?",new String[]{contact_id},null); 
        while(emailCurosr.moveToNext()){ 
         email = emailCurosr.getString(emailCurosr.getColumnIndex(EMAIL)); 
         output.append("\nEmail: "+email); 
        } 
        emailCurosr.close(); 
       } 
       output.append("\n"); 
      } 
      cursor.close(); 
     } 
     outputText.setText(output.toString()); 
    } 

내가 cursor.getCount에서 디버깅 및 "544 접촉이"발견 된 참조 내 코드입니다. 그러나 int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER)));은 항상 0입니다.

나는 이유를 모른다.

+0

'cursor.getColumnIndex (HAS_PHONE_NUMBER)'가 자체적으로 해결하는 것과'cursor.getString()'이 이전 해결책으로 해결할 내용으로 업데이트 할 수 있습니까? –

답변

0

명단.

나는 단순화와 함께 밖으로 번호를 행을 계산하고 내 휴대 전화에서 실행할 수 있도록 코드를 수정 : 나는, 그래서

 MainActivity D foundWithNumber: 184 
        D foundWithOutNumber: 303 

:

private void readContacts() { 
    Cursor c = getContentResolver() 
      .query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    int foundWithNumber = 0; 
    int foundWithOutNumber = 0; 
    if (c.moveToFirst()) { 
     int idHasNumber = c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER); 
     do { 
      boolean hasNumber = c.getInt(idHasNumber) > 0; 
      if (hasNumber) { 
       ++foundWithNumber; 
      } else { 
       ++foundWithOutNumber; 
      } 
     } while (c.moveToNext()); 
    } 
    Log.d(TAG, "foundWithNumber: " + foundWithNumber); 
    Log.d(TAG, "foundWithOutNumber: " + foundWithOutNumber); 
    c.close(); 
} 

내 휴대 전화에 OUPUT은 추측하면 잘못된 행을 보았을뿐입니다. ;)

+0

예, 정확히 잘못된 행이긴하지만 Gmail에서 동기화 할 때 올바른 행이 어디에 있는지 어떻게 알 수 있습니까? – binhnt

+0

글쎄,'HAS_PHONE_NUMBER = 1' 행을 선택할 수 있습니다. – flx

0

당신은이 nead 안드로이드에서 읽기 연락처 권한을 추가 연락처가 여러 컬럼에 저장되어 있기 때문에 당신은, 수없이 많은에게 연락처를 가있을 수 있습니다

android.permission.READ_CONTACTS

관련 문제