Challenge 2 Always shows the First contact number

Even if i choose a different contact name when i click my call button it displays the number of the first contact in my Contacts App. Foll is the code of my call button

calltheSuspect.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri phoneURI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
String[] projection = {ContactsContract.CommonDataKinds.Phone.NUMBER};
// String selection = ContactsContract.CommonDataKinds.Phone._ID + “= ?”;
// String[] selectionArgs = {Long.toString(crime.getPhoneId())};

            Cursor c = getActivity().getContentResolver().query(phoneURI, projection, null, null, null);
            int indexNumber = c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            try {

                if (c.getCount() == 0) {

                String phoneNumber = c.getString(indexNumber);
                Uri call = Uri.parse("tel:" + phoneNumber);
                Intent callNumber = new Intent(Intent.ACTION_DIAL, call);
                callNumber=Intent.createChooser(callNumber,"Call Number");

            } finally {

You aren’t using your selection/selectionArgs. This means your query is selecting every phone number on the device. Then, in your code, you move to the first record and that’s what you pull out.

You need to add back in the selection args to the query so that you filter out the results based on the contact that the user chose.

Thanks for the reply.Got the solution

Hi,I used your method,but when I ran the software, there was a privilege limit, but I registered in AndroidManifest.xml. Should I dynamically apply for permissions?

Can you post your code and the screenshot saying privilege limit?

Thanks,The problem was solved after I added the permissions to the dynamic

Can you please tell me which permission did u add?

I dynamically added READ_CONTACTS and CALL-PHONE permissions。Since Android 6, these permissions are listed as dangerous permissions, without dynamic add these two permissions, the program will flash back, plus after you can work properly