0

I tried almost every other answer or Code out there but my problem still persists. The phone number getting retrieved of contacts are arbitrary 3-4 digit numbers and not the exact phone number of the contact. When i go ahead and dial the number it dials to "6855". Please help.

the code

if(mWasButtonClicked){
        String[] projections = new String[] {ContactsContract.CommonDataKinds.Phone.NUMBER};
        String selectionClause = ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?";
        String[] selectionArgs = new String[] {mContactID};


        Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,projections,selectionClause,selectionArgs,null,null);

        try {
            if(c.getCount() == 0){return;}

            if(c!=null){
                while(c.moveToNext()){

                    c.moveToFirst();
                    phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

                    Log.d("TEST",phoneNumber);
                }
            }
        }finally {
            c.close();
        }
    }

for the call button

mCallButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Uri uri = Uri.parse("tel:"+phoneNumber);
            startActivity(new Intent(Intent.ACTION_CALL,uri));
        }
    });

For retrieving the contact ID

 Uri content_uri = data.getData();

        String[] projection = new String[] {ContactsContract.Contacts.DISPLAY_NAME,ContactsContract.Contacts._ID};

        Cursor c = getContentResolver().query(content_uri,projection,null,null,null,null);

        try {
            if(c.getCount() == 0){return;}

            if(c!=null){
                while(c.moveToNext()){

                    c.moveToFirst();
                    String name = c.getString(0);
                    mGetNameButton.setText(name);
                    mContactID = c.getString(1);
                    Log.d("TEST",mContactID);
                }
            }
        }finally {
            c.close();
        }

and the logs

2020-01-10 05:41:38.284 27904-27904/com.example.contactsuri D/TEST: 84
Hemant N. Karmur
  • 840
  • 1
  • 7
  • 21
  • Does this answer your question? [How to get contacts' phone number in Android](https://stackoverflow.com/questions/11218845/how-to-get-contacts-phone-number-in-android) – Daniele Segato Jan 13 '20 at 10:20

1 Answers1

0

I added the code below needed to retrieved the phone number of the device and call button. hope i help you to solve the problem

private static final int PERMISSION_REQUEST_CODE = 1; //permission
String wantPermission = Manifest.permission.READ_PHONE_STATE; //permission
String getMyMobilePhoneNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //call this function
    getTheMobilePhoneNumber();

    //to able see the phone number
    TextView getPhone = findViewById(R.id.getPhone);
    getPhone.setText(getMyMobilePhoneNumber);

    //add permission
    if (!checkPermission(wantPermission)) {
        requestPermission(wantPermission);
    } else {
        Log.d("TAG", "Phone number: " + getTheMobilePhoneNumber());
        getMyMobilePhoneNumber = getTheMobilePhoneNumber();
    }


    //for calling
    Button btnCallNum = findViewById(R.id.btnCall);
    btnCallNum.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentCall = new Intent(Intent.ACTION_DIAL);
            intentCall.setData(Uri.parse("tel:" + getMyMobilePhoneNumber));
            startActivity(intentCall);
        }
    });
}


//get your phone number by calling this
public String getTheMobilePhoneNumber() {
    TelephonyManager telMngr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);

    if (ActivityCompat.checkSelfPermission(this, wantPermission) != PackageManager.PERMISSION_GRANTED) {
        return null;
    }
    getMyMobilePhoneNumber = telMngr.getLine1Number();

    return getMyMobilePhoneNumber;
}

private void requestPermission(String permission) {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
        Log.d("TAG NUM", "Phone state permission allows us to get phone number. Please allow it for additional functionality. ");
    }
    ActivityCompat.requestPermissions(this, new String[]{permission}, PERMISSION_REQUEST_CODE);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d("TAG", "Phone number: " + getTheMobilePhoneNumber());
            } else {
                Log.d("TAG NUM", "Permission Denied. We can't get phone number ");
            }
            break;
    }
}

private boolean checkPermission(String permission) {
    if (Build.VERSION.SDK_INT >= 23) {
        int result = ContextCompat.checkSelfPermission(this, permission);
        return result == PackageManager.PERMISSION_GRANTED;
    } else {
        return true;
    }
}

}

also add this in you AndroidManifest

 <uses-permission android:name="android.permission.READ_PHONE_NUMBERS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
No Name
  • 472
  • 1
  • 6
  • 19