2017-04-15 18 views
-2

/これは私のオーバーライド機能です。私はちょうど電話帳から名前を取得するときにうまく動作します。しかし、私は連絡先番号を取得しようとすると、アプリケーションがクラッシュします。/アンドロイドの電話帳から連絡先番号を取り出す方法は?

@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

はいマニフェストに追加しました。 –

関連する問題