I am developing an application that required to access contacts in phone. this method is executed inside a thread. But when app start for the first time it crashes and says android.permission.READ_CONTACTS
or android.permission.READ_CONTACTS
required as error. as soon as press on OK in on error pop up dialog box. it restart it self asking for permission and works fine.
here's the code inside fragment on onCreateView method to check whether permission already has been granted.
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_CONTACTS)
== PackageManager.PERMISSION_GRANTED) {
Runnable r = new Runnable() {
@Override
public void run() {
getContacts();
}
};
Thread thread = new Thread(r);
thread.start();
}else{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
Toast.makeText(getActivity(),"Read contacts permission is required to function app correctly",Toast.LENGTH_LONG)
.show();
}
requestPermissions(new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_READ_CONTACTS);
}
}
here's the onRequestPermissionsResult method.
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case REQUEST_READ_CONTACTS :
Runnable r = new Runnable() {
@Override
public void run() {
getContacts();
}
};
Thread thread = new Thread(r);
thread.start();
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
here's the code for get contact method.
public void getContacts() {
ContentResolver cr = getActivity().getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
if (cur != 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));
String imgPath = cur.getString(cur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
Cursor ncur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);
if (ncur != null) {
while (ncur.moveToNext()) {
String phoneNumber = ncur.getString(ncur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
String reDefinedPhoneNumber = "";
if (phoneNumber.contains("-")) {
String[] split = phoneNumber.split("-");
for (String k : split) {
reDefinedPhoneNumber = reDefinedPhoneNumber.concat(k);
}
} else if (phoneNumber.contains(" ")) {
String[] split = phoneNumber.split(" ");
for (String k : split) {
reDefinedPhoneNumber = reDefinedPhoneNumber.concat(k);
}
} else {
reDefinedPhoneNumber = phoneNumber;
}
Contact contact = new Contact();
contact.setId(contact.getId());
contact.setName(name);
contact.setNumber(reDefinedPhoneNumber);
contact.setImgPath(imgPath);
contacts.add(contact);
}
ncur.close();
}
}
}
}
cur.close();
}
}
Here's AndroidManifest.xml code.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shan.chathuranga.smsscheduler">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
</manifest>
what am I doing wrong. works fine in below marshmallow.