Android运行时权限错误

时间:2016-11-07 15:11:08

标签: android runtime-permissions

我正在开发一个需要访问手机中联系人的应用程序。此方法在线程内执行。但是,当应用首次启动时,它会崩溃并将android.permission.READ_CONTACTSandroid.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>
我在做错了什么。在棉花糖下面工作正常。

0 个答案:

没有答案