How to call Android contacts list AND Select one phone number from its details screen?

前端 未结 6 1220
余生分开走
余生分开走 2020-12-01 03:06

I have read the already posted solutions, but they dont tell how do I use system\'s contact details screen to select any ONE number to use? I am developing an sms sending an

6条回答
  •  抹茶落季
    2020-12-01 03:43

    There is a problem in the accepted answer. If the number selected contains spaces within it
    i.e. 85 29 948789 then it will show only 85 (until first space).

    so use the below code to rectify this problem :)

    Intent intent1 = new Intent(Intent.ACTION_PICK);
    intent1.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE);
                startActivityForResult(intent1, 1);
    

    and in onActivityResult

        Uri contactUri = data.getData();
    
        String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME};
    
    
        Cursor cursor = getContentResolver()
                .query(contactUri, projection, null, null, null);
        cursor.moveToFirst();
    
    
        int numberColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
        int nameColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        String number = cursor.getString(numberColumn);
        String name = cursor.getString(nameColumn);
    

提交回复
热议问题