我正在开发一个需要访问电话联系人的应用程序。此方法在线程内执行。但是,当应用程序第一次启动时,它会崩溃并说android.permission.READ_CONTACTS
或 android.permission.READ_CONTACTS
需要作为错误。只要在错误弹出对话框中按确定。它重新启动它自己请求许可并且工作正常。
这是 onCreateView 方法片段内的代码,用于检查是否已授予权限。
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);
}
}
这是 onRequestPermissionsResult 方法。
@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);
}
}
这是获取联系方法的代码。
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();
}
}
这是 AndroidManifest.xml 代码。
<?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>
我究竟做错了什么。在棉花糖下面工作正常。