Android 系列 2.2在运行时请求Android权限

263 篇文章 2 订阅
164 篇文章 0 订阅
2.2在运行时请求Android权限


问题
在Android 6及更高版本中,除了在清单中指定它们之外,还必须在运行时检查权限。

“危险”资源是可能影响用户存储的信息或隐私等的资源。要访问受“危险”权限保护的资源,您必须:
•检查用户是否在访问资源之前已经授予权限
•如果先前未授予权限,则明确请求用户的权限
•有另一个操作过程,所以如果未授予权限,应用程序不会崩溃
讨论

在访问需要权限的资源之前,必须首先检查用户是否已经授予权限。 调用Activity方法的checkSelfPermission(权限)。 它将返回PERMISSION_GRANTED或PERMISSION_DENIED。

if (ActivityCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
// If you get here then you have the permission and can do some work.
} else {
// see below
}


如果上述检查表明尚未授予权限,则必须通过调用Activity方法requestPermissions()显式请求权限:
void requestPermissions(String [] permissions,int requestCode)
由于这将与用户交互,它是一个异步请求。 您必须覆盖Activity方法onRequestPermissionsResult()以获取响应的通知
public void onRequestPermissionsResult(int requestCode,String [] permissions,int [] grantResults);
例如:

// Unique request code for the particular permissions request
private static int REQUEST_EXTERNAL_STORAGE = 1;
...
// Request the permission from the user
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE },
REQUEST_EXTERNAL_STORAGE);
// Callback handler for the eventual response:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
boolean granted = true;
if (requestCode == REQUEST_EXTERNAL_STORAGE) {
// Received permission result for external storage permission.
Log.i(TAG, "Received response for external storage permission request.");
// Check if all the permissions have been granted
if (grantResults.length > 0 ) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
granted = false;
}
}
} else {
granted = false;
}
}
...
// If granted is true: carry on and perform the action
// Calling checkSelfPermission() will now return PackageManager.PERMISSION_GRANTED


通常一个好主意是向用户提供有关为什么需要权限的更多信息。 为此,你调用Activity方法boolean shouldShowRequestPermissionRationale(String permission)。 如果用户以前拒绝授予权限,那么此方法将返回true,您可以显示有关为什么应该授予权限的额外信息。

if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Provide an additional explanation to the user if the permission was not granted
// and the user would benefit from additional context for the use of the permission.
Log.i(TAG, "Displaying permission rationale to provide additional context.");
Snackbar.make(mLayout, R.string.external_storage_rationale,
Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.ok, new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_STORAGE);
}
}).show();



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值