I'm trying to write a simple app, that will change Android(my ver is 2.3.3) settings, but I have some problems. My API level is 10, so I use System.Secure, because Settings.System are deprecated and Settings.Global are in newer APIs.
When I use this code, app crashes :( :
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);
}
}
});
}
}
Of course I use permissions in manifest
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />
I also tried this code with android:onClick="toggle" but it doesn't work too :/
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);
}
}
}
Does anyone has an idea how to make it work?
You can not change the values of secure table, unless your app is bundled with the system (RTM):