gpt4 book ai didi

Android Marshmallow 权限错误

转载 作者:行者123 更新时间:2023-11-30 01:24:00 25 4
gpt4 key购买 nike

我正在为我的应用程序添加某些权限,例如允许访问相机,并且一切正常。但是当我最小化应用程序并禁用权限然后打开应用程序时,应用程序崩溃而不要求我重新启用权限(直到我关闭应用程序然后打开它)。我该如何修复此错误,以便应用程序不会崩溃并再次请求权限或安全重启以请求权限。

这是我的代码

在主要 Activity 中:

创建时:

 if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA},
GlobalVariables.MY_PERMISSIONS_REQUEST_CAMERA);
}


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode)
{

case GlobalVariables.MY_PERMISSIONS_REQUEST_CAMERA:
{
if (grantResults.length <= 0
|| grantResults[0] != PackageManager.PERMISSION_GRANTED) {
globalVariables.ShowOKAlert("Error","Please Accept All Requested Permissions or the app wont function properly",this,false);
}
return;
}
}
}

Activity 实现 ActivityCompat.OnRequestPermissionsResultCallback

最佳答案

第一步:

 String[] permissions = new String[]{ 
Manifest.permission.INTERNET,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
};

第 2 步:-

private boolean checkPermissions() {
int result;
List<String> listPermissionsNeeded = new ArrayList<>();
for (String p : permissions) {
result = ContextCompat.checkSelfPermission(this, p);
if (result != PackageManager.PERMISSION_GRANTED) {
listPermissionsNeeded.add(p);
}
}
if (!listPermissionsNeeded.isEmpty()) {
ActivityCompat.requestPermissions(this, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), 100);
return false;
}
return true;
}

第 3 步:

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == 100) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// do something
}
return;
}
}

关于Android Marshmallow 权限错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36693564/

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