0

I'm developing a game, and I've put a toggle button on the settings activity to turn off and on the music in the game activity.

Here's the Settings.java file:

public class Settings extends Activity implements OnCheckedChangeListener{

ToggleButton tbMusic;
boolean music;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    tbMusic = (ToggleButton)findViewById(R.id.tbMusic);

    tbMusic.setOnCheckedChangeListener(this);
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(tbMusic.isChecked()) {
        music = true;
    } else {
        music = false;  
    }
    }
    }

And the Game.java file (For the moment I only have this):

    public class Game extends Activity {

MediaPlayer menuSong;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.game);

    menuSong = MediaPlayer.create(Menu.this, R.raw.menu);
    menuSong.setLooping(true);
    menuSong.start(); 
}


@Override
protected void onStop() {
    super.onStop();
    menuSong.stop();
    }
}

Can you explain me or put an example about how I can do so that when the ToggleButton is checked, the music in the game is off?

Thank you!

4

1 回答 1

1

There is a lot different solutions for your problem. I am going to give you 2 solutions:

1 - Use the Application class. All the app could implement a Application class in this way:

public class ClassName extends Application {
}

It is a super class which from whatever Activity of the app, this class is accesible using this command:

getApplication();

Ok, you can read something in internet about how build this application class. The solution for your problem would be to create a variable in the application class like:

public class ClassName extends Application 
{
   private boolean isSoundEnabled;

   public boolean getSoundEnabled() { return isSoundEnabled; }
   public void setSoundEnabled(boolean isEnabled) { isSoundEnabled = isEnabled;}

@Override
    public void onCreate() {
        super.onCreate();
            isSoundEnabled = true;  
}

In this way, in your class Settings you can set this variable like:

 @Override
 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(tbMusic.isChecked()) {
       getApplication().setSoundEnabled ( true );
    } else {
       getApplication().setSoundEnabled ( false ); 
    }
}

And everytime that you open your class Game you can check how this variable is setted :

 getApplication().getSoundEnabled();

2 - The second solution for me is better because you save this information in memory, so everytime that the user open the app/game, the app can remember how the user setted in the pass this variable.

This is the way how we can remember flags, settings stuffs and recover this information after to close and open the app. We are going to use the SharedPreferences object. It is used to save values like int, float, boolean, etc (simple data objects) in the memory of the phone.

So in the Settings class, everytime that the user set the sound you can save the value:

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

if(tbMusic.isChecked()) {
       prefs.edit().putBoolean ("IS_ENABLED_VARIABLE", true ).commit();
    } else {
       prefs.edit().putBoolean ("IS_ENABLED_VARIABLE", false ).commit();
    }

And in you Game class you can recover this value using this code:

SharedPreferences prefs = this.getSharedPreferences("com.example.app", Context.MODE_PRIVATE);

boolean isEnabled = prefs.getBoolean("IS_ENABLED_VARIABLE", true);
//The second parameter (true) is the value by default if the variable with the name IS_ENABLED_VARIABLE is not find for the app.

IS_ENABLED_VARIABLE --> It is the name of the variable, you need use the same to save or recover it. you can use whatever name that you wish.

com.example.app --> it is the name that you call to the sharedprefences area/directory into the memory of your phone. you can use the name that you wish.

Hope to be helpful and if it is, vote me, please!

于 2013-09-12T16:03:55.533 回答