Marshmallow中的运行时权限弹出单击问题已更新的设备

时间:2016-07-04 11:01:44

标签: android android-layout android-6.0-marshmallow

我为Marshmallow设备集成了READ PHONE STATE的Android运行时权限。这个实现工作正常,弹出窗口在我的应用程序中显示allow / deny选项。

我可以点击普通棉花糖设备的允许/拒绝按钮。但是在更新的Android设备(从Lollipop到Marshmallow)的情况下,允许按钮单击不起作用的初始时间。此问题已在Nexus 5和Nexus 7中进行测试和复制。我们是否需要为更新的操作系统创建额外的内容?还是棉花糖问题?

请检查完整代码:

    private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 101;

if (Build.VERSION.SDK_INT >= 23) {
   if(mActivity.checkSelfPermission(Manifest.permission.READ_PHONE_STATE) !=PackageManager.PERMISSION_GRANTED) {
                                if (this.shouldShowRequestPermissionRationale(
                                        Manifest.permission.READ_PHONE_STATE)) {
                                    showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
                                } else {
                                    this.requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                                            MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);
                                }
                            } else {
                                handleLoginAPI();
                            }
                        } else {
                            handleLoginAPI();
                        }

     private void handleLoginAPI() {
      if (super.isNetworkConnectionAvailable(mActivity)) {
        // Api Call from here.. 
      }else{
        // No Nw Connection.
      }

    }

    @Override
        public void onRequestPermissionsResult(int requestCode,
                                               String permissions[], int[] grantResults) {
            switch (requestCode) {
                case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
                    // If request is cancelled, the result arrays are empty.
                    if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        handleLoginAPI();
                    } else if (grantResults.length > 0
                            && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                        if (this.shouldShowRequestPermissionRationale(
                                Manifest.permission.READ_PHONE_STATE)) {
                            showExplanationDialog(mActivity, getString(R.string.dialog_message_phone_state));
                        }
                    }
                    return;
                }
            }
        }

3 个答案:

答案 0 :(得分:1)

在请求许可之前不要致电shouldShowRequestPermissionRationale()。该呼叫旨在在用户拒绝请求后进行,通常采用onRequestPermissionsResult()方法。如果你的应用需要在它要求之前解释为什么它需要权限,应用程序的内部逻辑需要决定。

答案 1 :(得分:1)

 private void setCheck()
    {
        int hasWriteContactsPermission = 0;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
            hasWriteContactsPermission = checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE);
            if (hasWriteContactsPermission != PackageManager.PERMISSION_GRANTED) {
                if (!shouldShowRequestPermissionRationale(android.Manifest.permission.READ_PHONE_STATE)) {
                    showLocationRationleDalog();
                    return;
                }
                return;
            } else {
                handleLoginAPI();
            }
        } else {
            handleLoginAPI();
        }
    }


    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        switch (requestCode) {
            case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length == 1
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    handleLoginAPI();
                } else {
                    Log.i("Permission denied");
                }
                return;
            }
        }
    }

答案 2 :(得分:0)

这是Android操作系统更新问题。 Nexus设备报告了此问题。 https://code.google.com/p/android/issues/detail?id=213120。该问题可以在其他Android原生应用程序中重现,如Google地图,Gmail等。