gpt4 book ai didi

java.lang.SecurityException : Permission denial: writing to settings requires android. 权限.WRITE_SETTINGS

转载 作者:行者123 更新时间:2023-11-29 03:01:48 31 4
gpt4 key购买 nike

这是我在平台 5.0.0 上构建飞行计划模式开/关的代码下面是我的 Activity.java 源代码请建议我需要在 manifeast.xml 中做哪些更改以获得访问权限。

package com.rjil.airplanemodeonoff;

import android.content.Intent;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class MainActivity extends AppCompatActivity {
// constants
static final String STATUS_ON = "Airplane Mode: ON";
static final String STATUS_OFF = "Airplane Mode: OFF";

static final String TURN_ON = "Turn ON";
static final String TURN_OFF = "Turn OFF";

// controls
TextView TVAirplaneMode;
ToggleButton tBAirplane;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load controls
TVAirplaneMode = (TextView)findViewById(R.id.TVAirplaneMode);
tBAirplane = (ToggleButton)findViewById(R.id.tBAirplane);
// update UI at first time loading
updateUI(isAirplaneMode());
// set click event for button
tBAirplane.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
if (state) toggleAirplaneMode(0, state);
else toggleAirplaneMode(1, state);
// update UI to new state
updateUI(!state);
}
});
}
public void toggleAirplaneMode(int value, boolean state) {
// toggle airplane mode
//check the version
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //if less than verson 4.2
Settings.System.putInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, value);
} else {
Settings.Global.putInt(
getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, value);
}
// broadcast an intent to inform
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", !state);
sendBroadcast(intent);
}

public void updateUI(boolean state) {
//set text according to state
if(state) {
TVAirplaneMode.setText(STATUS_ON);
tBAirplane.setText(TURN_OFF);
} else {
TVAirplaneMode.setText(STATUS_OFF);
tBAirplane.setText(TURN_ON);
}
}


public boolean isAirplaneMode() {
//check the version
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {//if less than verson 4.2
return Settings.System.getInt(getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) != 0;
} else {
return Settings.Global.getInt(getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;

}
}



}

最佳答案

WRITE_SECURE_SETTINGS 不供第三方应用程序使用。无论您是在根目录还是生产版本上,系统/固件之外的任何应用都无法获得该权限。

请查看this回答

关于java.lang.SecurityException : Permission denial: writing to settings requires android. 权限.WRITE_SETTINGS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34412194/

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