如何添加多个权限?一个来自清单,一个来自运行时
How to add multiple permissions? one from manifest and one from runtime at same time
我在清单文件中添加了相机权限。因此,每当构建我的 apk 时,它都会请求相机许可。
然后在那之后我添加了一个运行时代码 write_storage_permission 但是当我构建应用程序时它首先请求相机许可然后如果我做一些需要存储许可的应用程序崩溃并且当我再次打开应用程序时它然后请求许可。
那么我该如何设置,无论何时构建我的应用程序,它都会请求相机许可(来自清单文件),然后紧接着请求 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();
}
您可以根据需要添加在继续之前要检查的权限。如果这解决了您的查询,请返回。
我在清单文件中添加了相机权限。因此,每当构建我的 apk 时,它都会请求相机许可。 然后在那之后我添加了一个运行时代码 write_storage_permission 但是当我构建应用程序时它首先请求相机许可然后如果我做一些需要存储许可的应用程序崩溃并且当我再次打开应用程序时它然后请求许可。
那么我该如何设置,无论何时构建我的应用程序,它都会请求相机许可(来自清单文件),然后紧接着请求 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();
}
您可以根据需要添加在继续之前要检查的权限。如果这解决了您的查询,请返回。