0

因此,当我调用 requestContactPermission 方法时,对话框正常显示,但是当我单击允许按钮时,片段关闭并且活动被迫重新创建?似乎是什么问题?日志不显示任何内容

   private void requestContactPermission() {

        if (shouldShowRequestPermissionRationale(Manifest.permission.READ_CONTACTS)) {
            getLoaderManager().initLoader(0, null, this);
        } else {
            requestPermissions(new String[]{Manifest.permission.READ_CONTACTS},
                    101);
        }

    }
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case 101:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    getLoaderManager().initLoader(0, null, this);
                }
                break;
        }
    }

我的主要问题是当我在模拟器中测试它时它工作正常但是当我在真实设备中测试它时,比如 Galaxy S7 edge 我遇到了这个问题

4

1 回答 1

0

试试这个。

 if (ContextCompat.checkSelfPermission(getActivity(),
                Manifest.permission.READ_CONTACTS)
                != PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "Requesting Runtime Permission Read contact");

            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{Manifest.permission.READ_CONTACTS},
                    101);
        } else {

      Log.d(TAG, "Previously User have provided Read contact access");

        }


 @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {

        switch (requestCode) {

            case 101: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                    Log.d(TAG, "Write External Storage Access granted");

                    downloadCurrentImage();

                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.

                } else {


                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
            }

            // other 'case' lines to check for other
            // permissions this app might request
        }
    }
于 2016-06-03T04:56:04.820 回答