2012-03-23 81 views
3

我目前正在開發一款安卓遊戲,並且我有一個選項屏幕,此時屏幕上有一個選項,其形式爲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); 
    } 
} 

}

+0

http://developer.android.com/guide/topics/data/data-storage.html – 2012-03-23 18:03:52

回答

6

SQLite不處理布爾類型。我總是使用smallint,並在場中放置1或0。請確保將其設置爲不接受空值,並將給定的用例設置爲默認值1或0。

這裏有一個例子:

CREATE TABLE "db"."boolean_example" ("boolean_example_field" smallint NOT NULL DEFAULT 0); 
+0

我同意你的+1, 存儲布爾在SQLite數據庫,你應該將其轉換爲0或1 – Houcine 2012-03-23 18:11:59

+0

感謝您的回覆,您能否給我一個這樣的例子? – 2012-03-23 18:32:24

2

要儲存的喜好更好的想法可能會被使用Shared Preferences。 這裏是例子:

SharedPreferences settings = getSharedPreferences("GameSettings", 0); 
    SharedPreferences.Editor editor = settings.edit();  
    editor.putBoolean("EnableMusic", mSilentMode); 
    editor.commit(); 

    ... 
    boolean isOn = settings.getBoolean("EnableMusic",true); 
+0

您好,感謝您的評論。我不認爲你可以編輯這個例子,並使用我上面發佈的一些代碼?對不起,我一直在嘗試這個,但我仍然不確定。謝謝。 – 2012-03-24 12:31:08