Settings.Secure WIFI_ON Android应用程序崩溃

时间:2014-01-07 01:17:10

标签: android crash wifi settings.settings

我正在尝试编写一个简单的应用程序,这将改变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);
        }
    }
});
}
}

当然我在清单中使用权限

<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);

}
}
}

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

1 个答案:

答案 0 :(得分:1)

您无法更改安全表的值,除非您的应用与系统捆绑在一起(RTM):

  

安全系统设置,包含应用程序可以读取但不允许写入的系统首选项。这些是用户必须通过系统UI或这些值的专用API明确修改的首选项,不直接由应用程序修改