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. In some phones it's not showing any errors or dialog box.
here's the code inside fragment on onCreateView method to check whether permission already has been granted
if (ContextCompat.checkSelfPermission(this, 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(this, "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 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. requestPermissions
method.
Here's the link to the repository https://chathurangaJ@bitbucket.org/chathurangaJ/sms-scheduler.git I'm stuck in this for 2 weeks please help.
I try to debug it. This error get as soon as application reach