java.lang.SecurityException:未被授予此权限:android.permission.WRITE_SETTINGS

时间:2017-12-03 16:31:45

标签: java android permissions

我要更改android中的一些系统设置,我使用这段代码:

java.lang.SecurityException: com.vpn.sabalan was not granted  this permission: android.permission.WRITE_SETTINGS.

此代码用于更改屏幕亮度使用亮度传感器,
但是在android 6中我得到了这个异常

 private void showBrightnessPermissionDialog(  )
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !android.provider.Settings.System.canWrite(this))
        {
            Intent intent = new Intent(android.provider.Settings.ACTION_MANAGE_WRITE_SETTINGS);
            intent.setData(Uri.parse("package:"+getPackageName()));
            startActivity(intent);
        }
    }

我可以使用这种方法获得用户的许可,但我需要获得程序许可,任何人都可以帮助我吗?

$sql="SELECT * FROM tbl_room WHERE roomid not in (SELECT b.roomid FROM tbl_booking b where(b.checkout BETWEEN '$checkin' and '$checkout') and (b.checkin BETWEEN '$checkin' and '$checkout'))";

2 个答案:

答案 0 :(得分:0)

按照文档中提供的步骤操作。这是非常彻底的。

https://developer.android.com/training/permissions/requesting.html

您所要做的就是请求权限,并覆盖onRequestPermissionsResult的回调以检查您是否得到它。如果你这样做,那么你很高兴。你仍然需要在你的清单中使用它,否则它将无效。

更新以根据您的评论显示详细信息。

public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback{

  private static final int REQUEST_WRITE_PERMISSION = 1001;

  @Override
  public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == REQUEST_WRITE_PERMISSION && grantResults[0] == PackageManager.PERMISSION_GRANTED) {            
        doFileWork();
    }else{
        //handle user denied permission, maybe dialog box to user
    }
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestPermission();
  }

  private void requestPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        requestPermissions(new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_PERMISSION);
    } else {
        doFileWork();
    }
  }
}

如果你真的想要走这条路线,那里还有许多好的库包装这个回调上下文,但它并不复杂。确保你的Manifest中也有写入权限。

答案 1 :(得分:0)

更新Android棉花糖和更高版本

您可以启动系统设置以授予写入系统设置。用户授予此权限后,您可以设置亮度而不会出现任何问题

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (Settings.System.canWrite(this)) {
                    Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS);
                    intent.setData(Uri.parse("package:" + getPackageName()));
                    startActivity(intent);
                }
            }