Android 运行时权限流程:我可以使用 ActivityCompat.requestPermissions 发送额外内容吗?
Posted
技术标签:
【中文标题】Android 运行时权限流程:我可以使用 ActivityCompat.requestPermissions 发送额外内容吗?【英文标题】:Android runtime permissions process: Can I send extras with ActivityCompat.requestPermissions? 【发布时间】:2018-01-24 18:11:37 【问题描述】:在请求授予 android 的危险权限期间,是否有任何方法可以发送 bundle/extras(就像我们对意图所做的那样)?
在为android授予危险权限的正常过程中,我们应该调用ActivityCompat.requestPermissions(...)
,根据documentation定义如下:
void requestPermissions (Activity activity, String[] permissions,int requestCode)
可以看出,在权限请求中没有发送任何额外信息的选项。目前我在我的活动中使用静态参数来在我的权限被授予后恢复它们。
我想知道有什么选择吗?
【问题讨论】:
【参考方案1】:我以这种方式包含意图,第一部分代码只是检查我们是否有 SD 卡
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
//.... write file into storage ...
System.out.println("SDK > BuildVersion TRUE");
else
requestPermissions(new String[]Manifest.permission.WRITE_EXTERNAL_STORAGE, 666); // Comment 26
System.out.println("go to requestPermissions");
onLoad();
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
case 666: // Allowed was selected so Permission granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
Snackbar s = Snackbar.make(findViewById(android.R.id.content),"Permission Granted",Snackbar.LENGTH_LONG);
View snackbarView = s.getView();
TextView textView = (TextView) snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.RED);
textView.setTextSize(18);
textView.setMaxLines(6);
s.show();
// do your work here
else if (Build.VERSION.SDK_INT >= 23 && !shouldShowRequestPermissionRationale(permissions[0]))
// User selected the Never Ask Again Option Change settings in app settings manually
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
alertDialogBuilder.setTitle("Change Permissions in Settings");
alertDialogBuilder
.setMessage("Click SETTINGS to Manually Set\n\n"+"Permissions to use Database Storage")
.setCancelable(false)
.setPositiveButton("SETTINGS", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, 1000); // Comment 3.
);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
else
// User selected Deny Dialog to EXIT App ==> OR <== RETRY to have a second chance to Allow Permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED)
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this, R.style.MyAlertDialogStyle);
alertDialogBuilder.setTitle("Second Chance");
alertDialogBuilder
.setMessage("Click RETRY to Set Permissions to Allow\n\n"+"Click EXIT to the Close App")
.setCancelable(false)
.setPositiveButton("RETRY", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
//ActivityCompat.requestPermissions((Activity) context, new String[]Manifest.permission.READ_EXTERNAL_STORAGE, Integer.parseInt(WRITE_EXTERNAL_STORAGE));
Intent i = new Intent(MainActivity.this,MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
)
.setNegativeButton("EXIT", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
finish();
dialog.cancel();
);
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
break;
;
【讨论】:
以上是关于Android 运行时权限流程:我可以使用 ActivityCompat.requestPermissions 发送额外内容吗?的主要内容,如果未能解决你的问题,请参考以下文章
权限拒绝:广播 Intent act=android.provider.Telephony.SMS_RECEIVED