Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Saving boolean values to a database (Beginner)

I am currently developing an android game and I have an options screen which at the moment has one Option in the form of a ToggleButton: Music on, or Music off.

I currently have this boolean in place, so the class checks if the music is currently playing and then this determines if the ToggleButton is checked or not:

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

So now I want to be able to send the values of True or False to a SQLite database I have set up. At this current moment in time, the only way I can do this is creating a TextView which changes value whether the ToggleButton is checked or not. The value in the TextView is then sent to the database successfully, but this creates problems such as not saving the current settings selected by the user.

Thank you to anyone who replies, if you require anymore code to help you answer I will supply it asap.

I have found some of the answers to be quite confusing, so I think it would be best if I just copied all of my code to here so you can get a better understanding:

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

}

like image 1000
Matthew Perrott Avatar asked Sep 12 '26 16:09

Matthew Perrott


1 Answers

SQLite doesn't handle a boolean type. I always just use smallint and put either a 1 or a 0 in the field. make sure you set it to not accept nulls, and set the default value to either 1 or 0, given your usecase.

here's an example:

CREATE  TABLE "db"."boolean_example" ("boolean_example_field" smallint NOT NULL  DEFAULT 0);
like image 101
Chris Drappier Avatar answered Sep 14 '26 04:09

Chris Drappier



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!