gpt4 book ai didi

android - 运行时权限 - 为已授予的权限请求权限

转载 作者:行者123 更新时间:2023-11-29 23:54:13 25 4
gpt4 key购买 nike

我编写了以下脚本来获取联系人和位置的多个运行时权限。我在布局中分别为联系人和位置使用了两个开关。如果我单击联系人开关,它会按预期请求对联系人和位置的许可。然后我从“应用程序权限”设置 Activity 中手动删除了位置权限。现在,如果我点击位置开关,它会再次询问联系人和位置。

enter image description here

我的问题是,既然我第一次授予了联系人权限,我可以在“应用程序权限”设置 Activity 中看到权限被授予,为什么又要请求联系人权限?即使我在 hasPermissions 方法中检查它。

public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1001;
String[] PERMISSIONS = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS,
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.basic_permissions);
CONTACTS_ACCESS_SWITCH = (Switch) findViewById(R.id.contactsPermissionSwitch);
LOCATION_ACCESS_SWITCH = (Switch) findViewById(R.id.locationPermissionSwitch);
CONTACTS_ACCESS_SWITCH.setOnClickListener(requestPermissionMethod);
LOCATION_ACCESS_SWITCH.setOnClickListener(requestPermissionMethod);
}

private void requestPermissionMethod() {
if (!hasPermissions(BasicPermissions.this, PERMISSIONS)) {
ActivityCompat.requestPermissions(BasicPermissions.this, PERMISSIONS, REQUEST_ID_MULTIPLE_PERMISSIONS);
}
}

private boolean hasPermissions(Context context, String... permissions) {
if (context != null && permissions != null) {
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(BasicPermissions.this, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}

list 文件:

<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

配置:

在 Android 6.0 android studio 模拟器设备上测试。

compileSdkVersion 27.

minSdkVersion 18

targetSdkVersion 27

最佳答案

由于代码是使用 Array 编写的,它请求对数组中所有值的权限,即使它已经被授予。所以我重写了代码以使用 ArrayList 而不是 Array。现在它按预期完美运行。

private  boolean requestPermissionMethod() {
int permissionContacts = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS);
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 (permissionContacts != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(Manifest.permission.READ_CONTACTS);
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),REQUEST_ID_MULTIPLE_PERMISSIONS);
return false;
}
return true;
}

关于android - 运行时权限 - 为已授予的权限请求权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50546299/

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