gpt4 book ai didi

android - 我们如何区分 Android M 的运行时权限中的从不询问和停止询问?

转载 作者:IT老高 更新时间:2023-10-28 13:20:57 28 4
gpt4 key购买 nike

当谈到 M Developer Preview 运行时权限时,根据 Google :

  1. 如果您以前从未请求过某个权限,请直接请求

  2. 如果你之前问过,用户说“不”,然后用户尝试做一些需要被拒绝权限的事情,你应该提示用户解释你为什么需要权限,然后再继续再次请求权限

  3. 如果您之前询问过几次,而用户说“不,并且停止询问”(通过运行时权限对话框上的复选框),您应该停止打扰(例如,禁用需要许可)

然而,我们只有一个方法,shouldShowRequestPermissionRationale(),返回一个boolean,我们有三种状态。我们需要一种方法来区分从未询问状态和停止询问状态,因为我们从 shouldShowRequestPermissionRationale() 中得到 false

对于第一次运行应用程序时请求的权限,这不是一个大问题。有很多方法可以确定这可能是您的应用程序的第一次运行(例如,SharedPreferences 中的 boolean 值),因此您假设这是第一次运行在您的应用程序中,您处于从未询问过的状态。

但是,运行时权限的部分愿景是您可能不会预先要求所有这些权限。当用户点击需要该权限的东西时,您可能只会在以后请求与边缘功能相关的权限。在这里,在我们突然需要请求另一个权限之前,应用程序可能已经运行了好几个月。

在这些情况下,我们是否应该跟踪我们是否自己请求了许可?还是我缺少的 Android M API 中的某些内容可以告诉我们之前是否询问过?

最佳答案

我知道我发布得很晚,但详细的示例可能对某人有所帮助。

我注意到的是,如果我们在 onRequestPermissionsResult() 回调方法中检查 shouldShowRequestPermissionRationale() 标志,它只显示两种状态。

状态1:-返回true:--任何时候用户点击拒绝权限(包括第一次。

状态 2:-返回 false :- 如果用户选择 s “不再询问。

这是一个具有多个权限请求的示例:-

应用在启动时需要 2 个权限。 SEND_SMS 和 ACCESS_FINE_LOCATION(都在 manifest.xml 中提到)。

应用程序一启动,就会同时请求多个权限。如果两个权限都被授予,则正常流程进行。

enter image description here

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if(checkAndRequestPermissions()) {
// carry on the normal flow, as the case of permissions granted.
}
}

private boolean checkAndRequestPermissions() {
int permissionSendMessage = ContextCompat.checkSelfPermission(this,
Manifest.permission.SEND_SMS);
int locationPermission = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
List<String> listPermissionsNeeded = new ArrayList<>();
if (locationPermission != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.ACCESS_FINE_LOCATION);
}
if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}

如果未授予一项或多项权限,activityCompat.requestPermissions() 将请求权限,控制转到 onRequestPermissionsResult() 回调方法。

您应该检查 onRequestPermissionsResult() 回调方法中 shouldShowRequestPermissionRationale() 标志的值。

只有两种情况:--

案例 1:-任何时候用户点击拒绝权限(包括第一次),它都会返回 true。所以当用户拒绝的时候,我们可以给出更多的解释并继续询问。

案例 2:-只有当用户选择“不再询问”时,它才会返回 false。在这种情况下,我们可以继续使用有限的功能,并引导用户从设置中激活权限以获得更多功能,或者如果权限对于应用程序来说是微不足道的,我们可以完成设置。

案例- 1

Case - 1

案例-2

Case - 2

@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
Log.d(TAG, "Permission callback called-------");
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {

Map<String, Integer> perms = new HashMap<>();
// Initialize the map with both permissions
perms.put(Manifest.permission.SEND_SMS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for both permissions
if (perms.get(Manifest.permission.SEND_SMS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "sms & location services permission granted");
// process the normal flow
//else any one or both the permissions are not granted
} else {
Log.d(TAG, "Some permissions are not granted ask again ");
//permission is denied (this is the first time, when "never ask again" is not checked) so ask again explaining the usage of permission
// // shouldShowRequestPermissionRationale will return true
//show the dialog or snackbar saying its necessary and try again otherwise proceed with setup.
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.SEND_SMS) || ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION)) {
showDialogOK("SMS and Location Services Permission required for this app",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
checkAndRequestPermissions();
break;
case DialogInterface.BUTTON_NEGATIVE:
// proceed with logic by disabling the related features or quit the app.
break;
}
}
});
}
//permission is denied (and never ask again is checked)
//shouldShowRequestPermissionRationale will return false
else {
Toast.makeText(this, "Go to settings and enable permissions", Toast.LENGTH_LONG)
.show();
// //proceed with logic by disabling the related features or quit the app.
}
}
}
}
}

}

private void showDialogOK(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", okListener)
.create()
.show();
}

关于android - 我们如何区分 Android M 的运行时权限中的从不询问和停止询问?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31928868/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com