-2

我想在android中獲取電話號碼作爲字符串,我成功地獲得了聯繫人並從他那裏獲取電話號碼,但日誌中的結果是data1,號碼是32821我不明白我的問題。如何從android中的聯繫人獲取電話號碼

這裏是我的代碼:

public void getContact(View view){ 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) { 
     // TODO: Consider calling 
     // ActivityCompat#requestPermissions 
     // here to request the missing permissions, and then overriding 
     // for ActivityCompat#requestPermissions for more details. 

     ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CONTACTS}, 10); 
//   return; 
    } 

    Intent contactsIntent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    this.pickContact = 1; 
    startActivityForResult(contactsIntent, this.pickContact); 
} 

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data); 

    if(reqCode == this.pickContact){ 
     if (resultCode == Activity.RESULT_OK) { 
      Log.d("ContactsH", "ResOK"); 
      Uri contactData = data.getData(); 
      Cursor contact = getContentResolver().query(contactData, null, null, null, null); 

      if (contact.moveToFirst()) { 
//     String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       String phoneNumber = ContactsContract.CommonDataKinds.Phone.NUMBER; 
       // TODO Whatever you want to do with the selected contact name and phone number. 

       Log.d("ContactsH", "Calling to:"+phoneNumber); 
       contact.close(); 
       this.callByNumber(phoneNumber); 
      } 
     } 
    }else{ 
     Log.d("ContactsH", "Canceled"); 
    } 
} 

你能幫助我嗎?

+4

的可能的複製[如何獲得接觸在Android手機號碼(https://stackoverflow.com/questions/11218845/how-to-get-contacts Android的電話號碼 –

+0

'String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));'不給你名字?你有沒有爲數字嘗試同樣的事情? –

+0

我很抱歉,但這不適合我,因爲它顯示相同的號碼,無論我選擇誰。 –

回答

1

感謝@Levon彼得羅

但我只是需要從他的link添加一部分,並把它複製到我的功能。

這是工作代碼:

@Override 
public void onActivityResult(int reqCode, int resultCode, Intent data){ 
    super.onActivityResult(reqCode, resultCode, data); 

    if(reqCode == this.pickContact){ 
     if (resultCode == Activity.RESULT_OK) { 
      Log.d("ContactsH", "ResOK"); 
      Uri contactData = data.getData(); 
      Cursor contact = getContentResolver().query(contactData, null, null, null, null); 

      if (contact.moveToFirst()) { 
       String name = contact.getString(contact.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
       // TODO Whatever you want to do with the selected contact's name. 

       ContentResolver cr = getContentResolver(); 
       Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, 
         "DISPLAY_NAME = '" + name + "'", null, null); 
       if (cursor.moveToFirst()) { 
        String contactId = 
          cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
        // 
        // Get all phone numbers. 
        // 
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
          ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null); 
        while (phones.moveToNext()) { 
         String number = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
         int type = phones.getInt(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); 
         switch (type) { 
          case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: 
           // do something with the Home number here... 
           break; 
          case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: 
           // do something with the Mobile number here... 
           Log.d("ContactsH", number); 
           this.callByNumber(number); 
           break; 
          case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: 
           // do something with the Work number here... 
           break; 
         } 
        } 
        phones.close(); 
       } 
       cursor.close(); 
      } 
     } 
    }else{ 
     Log.d("ContactsH", "Canceled"); 
    } 
} 
相關問題