Below is my method to get the contact list of the user and storing it in my DB, it's working in KitKat, but in android 8.0, after getting the user's permissions to read contacts, then also I am not able to read the contact list, kindly guide me for any changes required.
private void getDetails(){
try {
Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
ContentResolver cr = getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
String[] projection = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
ContactsContract.CommonDataKinds.Phone.NUMBER };
Cursor names = getContentResolver().query(uri, projection, null, null, null);
int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
names.moveToFirst();
do {
String name = names.getString(indexName);
String number = names.getString(indexNumber);
JSONObject cust= new JSONObject();
cust.put("name",name);
cust.put("mobile",number);
obj.put(cust);
} while (names.moveToNext());
}
catch (Exception e){
e.printStackTrace();
}
}