Skip to content Skip to sidebar Skip to footer

I'm Trying To Extract Phone Number From Commondatakinds.phone Using Contact Id From Contactscontract,phone

The following error message is produced whenever user presses Yes button when requested permission to access contacts. java.lang.RuntimeException: Failure delivering result ResultI

Solution 1:

You are getting null when no contacts are found in particular _ID and use onRequestPermissionsResult() instead of onActivityResult().

Note: please check your ReadContact permission in Manifest and also give runtime permission.

Full Example:

publicclassMainActivityextendsAppCompatActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                requestPermissions(newString[]{Manifest.permission.READ_CONTACTS}, 0);
            }
        } else {
            getId();
        }
    }

    //get _ID and NAMEpublicvoidgetId() {
        ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                getNumbers(cr, id);
            }
        }
    }

    //get Numbers where ( _ID = CONTACT_ID).publicStringgetNumbers(ContentResolver cr, String Id) {
        List<String> Numbers = newArrayList<>();

        Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                newString[]{Id}, null);

        while (pCur.moveToNext()) {
            String phoneNumber = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            int phoneType = pCur.getInt(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));
            System.out.println("Contact No:==> " + phoneType);
            if (phoneType == ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE) {
                Numbers.add(phoneNumber);
            }
        }
        //return null when when no contacts found in specific ID otherwise it will generate error.if (Numbers.size() == 0) {
            returnnull;
        } else {
            System.out.println("Contact No:==> " + Numbers.get(0));
            returnNumbers.get(0);// get number from particular index and to get all numbers set datatype String to List<String>.
        }
    }

    @OverridepublicvoidonRequestPermissionsResult(int requestCode, @NonNullString[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 0) {
            getId();
        }
    }
}

Post a Comment for "I'm Trying To Extract Phone Number From Commondatakinds.phone Using Contact Id From Contactscontract,phone"