编辑:
好的,所以我更新了它以执行用户“android 开发人员”的建议,并且每次我进入应用程序时开关仍然保持未选中状态。
这是代码片段。顺便说一句,这是我的第一个应用程序,所以请轻松一点。我在学。
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PreferenceManager.getDefaultSharedPreferences(this).getBoolean("sw1",false);
}
public void onClick(View v) {}
public void sw1(View view) {
// Is the view now checked?
boolean checked = ((Switch) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.sw1:
if (checked) try {
//Toast.makeText(this, "SW1 Checked", Toast.LENGTH_SHORT).show();
Runtime.getRuntime().exec(new String[] { "su","-c","mod1" });
PreferenceManager .getDefaultSharedPreferences(this).edit().putBoolean("sw1",true).commit();
} catch (IOException e) {
e.printStackTrace();
}
else try {
//Toast.makeText(this, "SW1 NOT Checked", Toast.LENGTH_SHORT).show();
Runtime.getRuntime().exec(new String[] { "su","-c","mod1b" });
PreferenceManager .getDefaultSharedPreferences(this).edit().putBoolean("sw1",false).commit();
} catch (IOException e) {
e.printStackTrace();
break;}
}
}