2017-04-15 119 views
-2

/這是我的覆蓋功能。當我從電話簿中檢索名字時,它工作正常。但是,當我嘗試檢索聯繫人號碼時,該應用崩潰了。/如何從android中的電話簿中檢索聯繫人號碼?

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data) { 
    super.onActivityResult(reqCode, resultCode, data); 
    switch (reqCode) { 
     case (1) : 
      if (resultCode == Activity.RESULT_OK) { 
       Uri contactData = data.getData(); 
       Cursor c = getContentResolver().query(contactData, null, null, null, null); 
       if(c.moveToFirst()) { 
        String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID)); 
        String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)); 
        String name = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 
        if (hasPhone.equalsIgnoreCase("1")) 
        { 
         Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
           ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null); 
         phones.moveToFirst(); 
         String cNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         Toast.makeText(getApplicationContext(), cNumber, Toast.LENGTH_SHORT).show(); 

        } 
       } 
       c.close(); 
      } 
      break; 
    } 
} 
+0

張貼您的錯誤logcat。 –

+1

你有什麼錯誤?你應該發佈你的整個錯誤 – BlackHatSamurai

+0

我得到運行時錯誤: –

回答

0

我不知道你有什麼問題,但我對獲得的電話號碼,你可以試試代碼:

所有的
public String getPhone(Context context, String contactID) { 
     Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
     String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
     String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 

     // Query and loop for every phone number of the contact 
     Cursor phoneCursor = context.getContentResolver().query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contactID}, null); 
     String phoneNo = ""; 
     if(phoneCursor != null) { 
      while (phoneCursor.moveToNext()) { 
       phoneNo = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
      } 
      phoneCursor.close(); 
     } 
     return phoneNo; 
    } 
2

首先,創建POJO類。

public class ContactEntity { 

String name; 
String number; 
String id; 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getNumber() { 
    return number; 
} 

public void setNumber(String number) { 
    this.number = number; 
} 
} 

現在,檢索聯繫人。

ArrayList<ContactEntity> contactList = new ArrayList<>(); 

contactList = getcontactList(); 

public ArrayList<ContactEntity> getcontactList() //This Context parameter is nothing but your Activity class's Context 
{ 
    ArrayList<ContactEntity> allContacts = new ArrayList<>(); 
    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); 
    Integer contactsCount = cursor.getCount(); // get how many contacts you have in your contacts list 
    if (contactsCount > 0) { 

     while (cursor.moveToNext()) { 

      String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      String contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
      if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
       //the below cursor will give you details for multiple contacts 
       Cursor pCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
         ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", 
         new String[]{id}, null); 

       // continue till this cursor reaches to all phone numbers which are associated with a contact in the contact list 
       while (pCursor.moveToNext()) { 
        int phoneType = pCursor.getInt(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
        //String isStarred  = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.STARRED)); 
        String phoneNo = pCursor.getString(pCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

        //you will get all phone numbers according to it's type as below switch case. 


        ContactEntity entity = new ContactEntity(); 
        entity.setName(contactName); 
        entity.setNumber(phoneNo); 
        allContacts.add(entity); 

        //Log.e will print all contacts according to contact types. Here you go. 
        switch (phoneType) { 
         case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 
          Log.e(contactName + ": TYPE_MOBILE", " " + phoneNo); 
          break; 
         case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
          Log.e(contactName + ": TYPE_HOME", " " + phoneNo); 
          break; 
         case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 
          Log.e(contactName + ": TYPE_WORK", " " + phoneNo); 
          break; 
         case ContactsContract.CommonDataKinds.Phone.TYPE_WORK_MOBILE: 
          Log.e(contactName + ": TYPE_WORK_MOBILE", " " + phoneNo); 
          break; 
         case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER: 
          Log.e(contactName + ": TYPE_OTHER", " " + phoneNo); 
          break; 
         default: 
          break; 
        } 
       } 
       pCursor.close(); 
      } 
     } 
     cursor.close(); 

    } 
    return allContacts; 
} 
+0

我一直在這一行上發生錯誤 - getContentResolver()。query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);你會知道爲什麼嗎? –

+0

您是否爲清單文件中的聯繫人提供了權限.. @Gerard Grundy –

+0

是的,我將其添加到清單。 –

相關問題