2013-08-06 4 views
1

이 질문은 이미 질문 : Getting contact email by name하지만 이것은 도움이되지 않습니다. 나는 불행히도 그것이 작동하지 않는 이메일 주소를 얻기위한 다음과 같은 코드가 있습니다. 아무도이 문제를 해결할 수 있도록 도와줍니다.이메일 주소를 얻을 수 없습니다

ContentResolver cr = getContentResolver(); 
Cursor emailCur = cr.query( 
       ContactsContract.CommonDataKinds.Email.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Email.Display_Name + " = ?", 
       new String[]{name}, null); 
TextView tv2 = (TextView) findViewById(R.id.textView5); 

while (emailCur.moveToNext()) { 
       String email = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
       String emailType = emailCur.getString(
           emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); 
       tv2.setText(email); 
       Toast.makeText(getApplicationContext(), ""+email, Toast.LENGTH_LONG).show(); 
      } 
      emailCur.close(); 

또한 contact_id를 사용해 보았습니다.

답변

0

이것은

public static void getContactEmails(Context context) { 
     String emailIdOfContact = null; 
     int emailType = Email.TYPE_WORK; 
     String contactName = null; 


      ContentResolver cr = context.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(BaseColumns._ID)); 
        contactName = cur 
          .getString(cur 
            .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
        // Log.i(TAG,"....contact name....." + 
        // contactName); 

        cr.query(
          ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
          null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID 
            + " = ?", new String[] { id }, null); 

        Cursor emails = cr.query(Email.CONTENT_URI, null, 
          Email.CONTACT_ID + " = " + id, null, null); 
        while (emails.moveToNext()) { 
         emailIdOfContact = emails.getString(emails 
           .getColumnIndex(Email.DATA)); 
         // Log.i(TAG,"...COntact Name ...." 
         // + contactName + "...contact Number..." 
         // + emailIdOfContact); 
         emailType = emails.getInt(emails 
           .getColumnIndex(Phone.TYPE)); 


        } 
        emails.close(); 

       } 
      }// end of contact name cursor 
      cur.close(); 


    } 
+0

이미이 일을 시도하는 데 도움이 될 수 있습니다. 내 문제는 반복 루프가 0 인 동안 시작되지 않습니다. – Aravin

+0

매니페스트의 연락처에 액세스 할 수있는 권한을 추가 했습니까? –

+0

<사용 권한 android : name = "android.permission.READ_CONTACTS"/> –

관련 문제