gpt4 book ai didi

android - Settings.Secure WIFI_ON Android 应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 03:11:07 25 4
gpt4 key购买 nike

我正在尝试编写一个简单的应用程序,它将更改 Android(我的版本是 2.3.3)设置,但我遇到了一些问题。我的 API 级别是 10,所以我使用 System.Secure,因为 Settings.System 已被弃用,而 Settings.Global 在较新的 API 中。

当我使用这段代码时,应用程序崩溃了 :( :

public class OnOff extends Activity {
TextView msg;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//buttonLED = (ToggleButton) findViewById(R.id.toggleButton);
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggleButton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// The toggle is enabled
Settings.Secure.putInt(getContentResolver(), Settings.Secure.WIFI_ON, 1);
} else {
// The toggle is disabled
Settings.Secure.putInt(getContentResolver(), Settings.Secure.WIFI_ON, 0);
}
}
});
}
}

当然我使用 list 中的权限

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

我也用 android:onClick="toggle"尝试过这段代码,但它也不起作用:/

public class OnOff extends Activity {
TextView msg;
private ToggleButton buttonLED;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

public void toggle(View v){
if(buttonLED.isChecked()){
Toast.makeText(OnOff.this, "Setting ON", Toast.LENGTH_SHORT).show();
Settings.Secure.putInt(getContentResolver(), Settings.Secure.WIFI_ON, 1);
}
else{
Toast.makeText(OnOff.this, "Setting OFF", Toast.LENGTH_SHORT).show();
Settings.Secure.putInt(getContentResolver(), Settings.Secure.WIFI_ON, 0);

}
}
}

有没有人知道如何让它发挥作用?

最佳答案

您不能更改安全表的值,除非您的应用程序与系统 bundle 在一起 (RTM):

Secure system settings, containing system preferences that applications can read but are not allowed to write. These are for preferences that the user must explicitly modify through the system UI or specialized APIs for those values, not modified directly by applications.

关于android - Settings.Secure WIFI_ON Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20962307/

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