我目前正在開發一款安卓遊戲,並且我有一個選項屏幕,此時屏幕上有一個選項,其形式爲ToggleButton:Music on或Music off。Android:將布爾值保存到數據庫(初學者)
目前,我有這個布爾到位,因此類檢查如果音樂正在播放,然後這個判斷切換按鈕被選中與否:
private boolean isMyServiceRunning(String serviceCanonicalClassName) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceCanonicalClassName.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
所以現在我希望能夠發送True或False的值給我設置的SQLite數據庫。在當前這個時刻,我能做到的唯一方法是創建一個TextView,它可以改變ToggleButton是否被選中。然後將TextView中的值成功發送到數據庫,但這會產生問題,例如不保存用戶選擇的當前設置。
謝謝任何回覆的人,如果您需要更多代碼來幫助您回答,我會盡快提供。
我已經發現了一些問題的答案是相當混亂,所以我想如果我只是複製我的所有代碼,你在這兒可以更好地瞭解這將是最好的:
public class OptionsActivity extends Activity {
private boolean isMyServiceRunning(String serviceCanonicalClassName) {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceCanonicalClassName.equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Intent i; // Handles MyMusicService.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
final TextView tSound = (TextView) findViewById(R.id.textView2);
final TextView tJoin = (TextView) findViewById(R.id.textView3);
final Button saveBtn = (Button) findViewById(R.id.optSaveBtn);
final Button tblBtn = (Button) findViewById(R.id.tableBtn);
i=new Intent(this, MyMusicService.class);
final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);
final ToggleButton joinOption = (ToggleButton) findViewById(R.id.joinPref);
boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName());
boolean joinChecking = joinOption.isChecked();
soundOption.setChecked(musicPlays);
if(musicPlays==true){
tSound.setText("On");
}
if(musicPlays==false) {
tSound.setText("Off");
}
if(joinChecking==true){
tJoin.setText("Auto");
}
if(joinChecking==false){
tJoin.setText("Manual");
}
soundOption.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on clicks to control sound being on and off.
if(soundOption.isChecked()) {
Toast.makeText(OptionsActivity.this, "Music on.", Toast.LENGTH_SHORT).show();
startService(i);
}
else {
if(stopService(i)==true){
soundOption.setChecked(false);
stopService(i);
Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show();
}
}
}});
joinOption.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if(joinOption.isChecked()){
Toast.makeText(OptionsActivity.this, "Auto", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(OptionsActivity.this, "Manual", Toast.LENGTH_SHORT).show();
}
}
});
tblBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent shmoo = new Intent(OptionsActivity.this, SQLView.class);
startActivity(shmoo);
}
});
saveBtn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
switch (v.getId()){
case R.id.optSaveBtn: //Determine what will happen when the user presses the "Submit button".
boolean optionsWork = true;
try{
String sound = tSound.getText().toString();
String join = tJoin.getText().toString();
optionsDB entry = new optionsDB(OptionsActivity.this); //Creating a new instance of MasterMind game
entry.open();
entry.createOptionEntry(join, sound); //Passing both strings
entry.close();
}catch (Exception e){ //Creating an error message if for some reason the app cannot transfer data to the Database.
Toast.makeText(OptionsActivity.this, "Error", Toast.LENGTH_SHORT).show();
}
finally { //Creating an AlertDialog box when the user presses the Submit button.
if (optionsWork){
Toast.makeText(OptionsActivity.this, "Settings Saved", Toast.LENGTH_SHORT).show();
}
}
break;
}
}
});
}
protected void onDestroy() {
if (this.isFinishing()){
super.onDestroy();
stopService(i);
}
}
}
http://developer.android.com/guide/topics/data/data-storage.html – 2012-03-23 18:03:52