我正在为 android 6.0 Marshmallow 构建我的应用程序,它需要 WRITE_SETTTINGS 权限。经过搜索从这里我开始知道调用这个:
requestPermissions(new String[]{Manifest.permission.WRITE_SETTINGS},
101);
不会显示对话框权限。因此,基于 CommonsWare 解决方案,我们应该检查是否
Settings.System.canWrite()
返回 true 或 false。因此,我应该使用 ACTION_MANAGE_WRITE_SETTINGS
作为操作来调用 Activity。
但问题是,当我调用此活动时,它显示我的应用程序已被授予权限,尽管方法
Settings.System.canWrite()
返回 false。
我是否在这里遗漏了某些内容,或者我必须禁用它然后再次启用它。
在我使用 Android 6.0.1 (MMB29S) 的 Nexus 6 上,此代码:
if (!Settings.System.canWrite(this)) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:dummy"));
startActivity(intent);
}
仅当允许修改系统设置设置为禁用时,才打开设置。例如,全新安装后首次启动(即不重新安装)
编辑(请参阅评论):某些设备可能会出现与此代码相关的错误,其中
canWrite()
始终返回false
,无论设置的值是什么。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(context)) {
//Write code to feature for eg. set brightness or vibrate device
/* ContentResolver cResolver = context.getContentResolver();
Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS,brightness);*/
}
else {
showBrightnessPermissionDialog(context);
}
对话框:-
private static void showBrightnessPermissionDialog(final Context context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
final AlertDialog alert = builder.create();
builder.setMessage("Please give the permission to change brightness. \n Thanks ")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
alert.dismiss();
}
});
alert.show();
}
例如。亮度的完整代码。
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
public class BrightnessHelper {
public static void setBrightness(Context context, int brightness){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (Settings.System.canWrite(context)) {
//Write code to feature for eg. set brightness or vibrate device
ContentResolver cResolver = context.getContentResolver(); Settings.System.putInt(cResolver, Settings.System.SCREEN_BRIGHTNESS,brightness);
}
else {
showBrightnessPermissionDialog(context);
}
}
}
public static int getBrightness(Context context) {
ContentResolver cResolver = context.getContentResolver();
try {
return Settings.System.getInt(cResolver, Settings.System.SCREEN_BRIGHTNESS);
} catch (Settings.SettingNotFoundException e) {
return 0;
}
}
private static void showBrightnessPermissionDialog(final Context context) {
final AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setCancelable(true);
final AlertDialog alert = builder.create();
builder.setMessage("Please give the permission to change brightness. \n Thanks ")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
intent.setData(Uri.parse("package:" + context.getPackageName()));
// intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
alert.dismiss();
}
});
alert.show();
}
/*
private boolean checkSystemWritePermission(Activity activity) {
boolean retVal = true;
if (Build.VERSION.SDK_INT >= activity.Build.VERSION_CODES.M) {
retVal = Settings.System.canWrite(activity.getApplicationContext());
// Log.d(TAG, "Can Write Settings: " + retVal);
if(retVal){
Toast.makeText(activity, "Write allowed :-)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Write not allowed :-(", Toast.LENGTH_LONG).show();
FragmentManager fm = getFragmentManager();
PopupWritePermission dialogFragment = new PopupWritePermission();
dialogFragment.show(fm, getString(R.string.popup_writesettings_title));
}
}
return retVal;
}*/
}
将该方法写如下:
public void writePermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (!Settings.System.canWrite(getApplicationContext())) {
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS, Uri.parse("package:" + getPackageName()));
startActivityForResult(intent, 200);
}
}
}
然后在调用对话框之前调用方法(writePermission)
我希望这有帮助
事实证明,如果您在清单中声明了
CHANGE_NETWORK_STATE
,即使未授予权限,允许 WRITE_SETTINGS
的开关也将默认处于打开位置。您甚至不需要声明 WRITE_SETTINGS
即可遇到此错误。
我在开发 android 6 时遇到了类似的问题。这是因为,现在开发人员必须在运行时请求权限。我的解决方案在这里-
在 onCreate 中,显示权限对话框。假设该方法的名称是 showPermissionsDialog()。
//Global variable request code
private static final int WRITE_PERMISSION_REQUEST = 5000;
private void showPermissionsDialog() {
if (Build.VERSION.SDK_INT == 23) {
int hasWriteSettingsPermission = checkSelfPermission(Manifest.permission.WRITE_SETTINGS);
if (hasWriteSettingsPermission != PackageManager.PERMISSION_GRANTED) {
//You can skip the next if block. I use it to explain to user why I wan his permission.
if (!ActivityCompat.shouldShowRequestPermissionRationale(HomeActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)) {
showMessageOKCancel("You need to allow write settings",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(HomeActivity.this, new String[]{Manifest.permission.WRITE_SETTINGS}, WRITE_PERMISSION_REQUEST);
}
});
return;
}
//The next line causes a dialog to popup, asking the user to allow or deny us write permission
ActivityCompat.requestPermissions(HomeActivity.this, new String[]{Manifest.permission.WRITE_SETTINGS}, WRITE_PERMISSION_REQUEST);
return;
} else {
//Permissions have already been granted. Do whatever you want :)
}
}
}
//Now you only need this if you want to show the rationale behind
//requesting the permission.
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(HomeActivity.this).setMessage(message).setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null).show();
}
//This method is called immediately after the user makes his decision to either allow
// or disallow us permision.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case WRITE_PERMISSION_REQUEST:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//User pressed the allowed button
//Do what you want :)
} else {
//User denied the permission
//Come up with how to hand the requested permission
}
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}