Android Marshmallow:在授予权限后重新创建活动

时间:2016-06-03 02:33:49

标签: android android-fragments permissions runtime-permissions

因此,当我调用requestContactPermission方法时,对话框会正常显示,但是当我单击allow按钮时,片段关闭并且活动被迫重新创建?什么似乎是问题?对数剂量没有显示任何东西

   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我遇到了这个问题

1 个答案:

答案 0 :(得分: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
        }
    }