gpt4 book ai didi

android - 如何添加多个权限?一个来自 list ,一个来自运行时

转载 作者:行者123 更新时间:2023-11-29 02:21:47 25 4
gpt4 key购买 nike

我在 list 文件中添加了相机权限。因此,每当构建我的 apk 时,它都会请求相机许可。然后我为运行时 write_storage_permission 添加了一个代码,但是当我构建应用程序时它首先请求相机许可然后如果我做一些需要存储许可的应用程序崩溃并且当我再次打开应用程序时它然后请求许可。

那么我该如何设置,无论何时构建我的应用程序,它都会请求摄像头许可(来自 list 文件),然后紧随其后请求 write_external_storage 许可。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.download">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA" />


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data android:name="com.google.ar.core" android:value="required" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

最佳答案

首先,检查是否授予了您需要的权限。

        int rc1 = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int rc2 = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
int rc3 = ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
int rc4 = ActivityCompat.checkSelfPermission(this, Manifest.permission.INTERNET);

if (rc1 == PackageManager.PERMISSION_GRANTED &&
rc2 == PackageManager.PERMISSION_GRANTED &&
rc3 == PackageManager.PERMISSION_GRANTED &&
rc4 == PackageManager.PERMISSION_GRANTED) {
allGoodProceed();
} else {
requestPermission();
}

然后调用requestPermission方法。

    private void requestPermission() {
final String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA,
Manifest.permission.INTERNET};

boolean isPermissionDeniedPermanent = false;

for (int i = 0, len = permissions.length; i < len; i++) {
String permission = permissions[i];

if (ActivityCompat.checkSelfPermission(this, permissions[i]) == PackageManager.PERMISSION_DENIED) {
// user rejected the permission
boolean showRationale = shouldShowRequestPermissionRationale(permission);

if (showRationale) {
isPermissionDeniedPermanent = showRationale;
}
}
}

if (isPermissionDeniedPermanent) {
showPermissionsDialog();
} else {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.CAMERA)) {
ActivityCompat.requestPermissions(this, permissions, HANDLE_STORAGE_PERMISSION);
}
}
}

此外,重写onRequestPermissionsResult 方法来检查权限结果。

    @Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean isSomePermissionsMissing = false;
for (int i = 0, len = permissions.length; i < len; i++) {
if (ActivityCompat.checkSelfPermission(this, permissions[i]) == PackageManager.PERMISSION_DENIED) {
isSomePermissionsMissing = true;
}
}

if (isSomePermissionsMissing) {
showPermissionsDialog();
} else {
//all good proceed...
}
}


private void showPermissionsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Action required").setMessage("To proceed you have to grant some permissions");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

}
});

AlertDialog alertDialog=builder.create();
alertDialog.show();
}

您可以根据需要添加在继续之前要检查的权限。如果这解决了您的查询,请返回。

关于android - 如何添加多个权限?一个来自 list ,一个来自运行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55127821/

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