2012-11-04 2 views
0

전화에서 연락처를로드하는 데 도움이되는 사용 가능한 예제를 시도했습니다. 그것은 에뮬레이터에서 잘 작동하지만 실시간으로 모바일을 시도하면 충돌이 발생합니다. 아무도 완벽하게 작동하는 코드의 테스트 조각을 보낼 수 있습니까? 그럼 나는 내 코드와 비교할 수있다.연락처를로드 할 수 없습니다.

하나의 실패한 코드 예.

Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null); 
if (phones.getCount() > 0) 
{ 
    while (phones.moveToNext()) 
    { 
      name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

    } 
phones.close(); 

친절히 도움.

+0

도움이 될 것입니다 가정하자 : 그것이 작동하는 방법을 예로 코드의

cursor.moveToFirst(); 
여기

입니다 조각 :하지 null하다면 첫번째 전화로 포인터를 이동하려면이 행을 추가 스택 추적은 당신이 가진 것입니다. –

답변

0

하는 phones하지 있는지 확인 null. 추가하십시오

Uri contactData = data.getData(); 
    //Cursor cursor = managedQuery(contactData, null, null, null, null); 
    Cursor cursor = cr.query(contactData, null, null, null, null); 
    cursor.moveToFirst(); 
    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
    String id = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts._ID)); 



    if (Integer.parseInt(cursor.getString(
      cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
     Cursor pCur = cr.query(
           ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
           null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
           new String[]{id}, 
           null); 

     while (pCur.moveToNext()) { 
      String number = pCur.getString(pCur.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

      break; // ? we want only 1 value 
     } 
     pCur.close(); 
    } 

    cursor.close(); 

가 당신에게

0

첫째, 매니페스트 파일이 추가 :

<uses-permission android:name="android.permission.READ_CONTACTS"/> 

당신의 AndroidManifest.xml 파일에, 당신이 할 수있는 루프를 휴대폰 연락처를 통해이 같은 :

 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
     while (cursor.moveToNext()) { 
     String contactId = cursor.getString(cursor.getColumnIndex( 
     ContactsContract.Contacts._ID)); 
     String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
     if (Boolean.parseBoolean(hasPhone)) { 
    // You know it has a number so now query it like this 
     Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, null, null); 
     while (phones.moveToNext()) { 
     String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));     
     } 
     phones.close(); 
     } 

     Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId, null, null); 
     while (emails.moveToNext()) { 
     // This would allow you get several email addresses 
     String emailAddress = emails.getString( 
     emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); 
     } 
     emails.close(); 
      } 
     cursor.close(); 
관련 문제