w AlertDialog.Builder(this)
95 .setTitle("存储权限不可用")
96 .setMessage("请在-应用设置-权限-中,允许支付宝使用存储权限来保存用户数据")
97 .setPositiveButton("立即开启", new DialogInterface.OnClickListener() {
98 @Override
99 public void onClick(DialogInterface dialog, int which) {
100 // 跳转到应用设置界面
101 goToAppSetting();
102 }
103 })
104 .setNegativeButton("取消", new DialogInterface.OnClickListener() {
105 @Override
106 public void onClick(DialogInterface dialog, int which) {
107 finish();
108 }
109 }).setCancelable(false).show();
110 }
111
112 // 跳转到当前应用的设置界面
113 private void goToAppSetting() {
114 Intent intent = new Intent();
115
116 intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
117 Uri uri = Uri.fromParts("package", getPackageName(), null);
118 intent.setData(uri);
119
120 startActivityForResult(intent, 123);
121 }
122
123 //
124 @Override
125 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
126 super.onActivityResult(requestCode, resultCode, data);
127 if (requestCode == 123) {
128
129 if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
130 // 检查该权限是否已经获取
131 int i = ContextCompat.checkSelfPermission(this, permissions[0]);
132 // 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
133 if (i != PackageManager.PERMISSION_GRANTED) {
134 // 提示用户应该去应用设置界面手动开启权限
135 showDialogTipUserGoToAppSettting();
136 } else {
137 if (dialog != null && dialog.isShowing()) {
138 dialog.dismiss();
139 }
140 Toast.makeText(this, "权限获取成功", Toast.LENGTH_SHORT).show();
141 }
142 }
143 }
144 }
145 }
|