自静音的ImageButton使用布尔不断变化真 [英] custom mute ImageButton using boolean keeps changing to true

查看:147
本文介绍了自静音的ImageButton使用布尔不断变化真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图与2个不同的背景图片(和关闭声音)静音的ImageButton。

I am trying to make a mute ImageButton with 2 different background images (sound on and off).

下面是我的code

public class StartActivity extends Activity implements OnClickListener{

public boolean IsAudioOn = true;

static AudioManager amanager;
ImageButton playBtn, muteBtn, rulesBtn;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_start);

    playBtn = (ImageButton) findViewById(R.id.playBtn);
    playBtn.setOnClickListener(this);
    muteBtn = (ImageButton) findViewById(R.id.muteBtn);
    muteBtn.setOnClickListener(this);
    rulesBtn = (ImageButton) findViewById(R.id.rulesBtn);
    rulesBtn.setOnClickListener(this);          
}

public void onClick(View v) {
    Intent i;   

    switch (v.getId())
    {
    case R.id.playBtn : 
                        i = new Intent(this, QuizActivity.class);
                        startActivity(i);
                        finish();
                        break;
    case R.id.rulesBtn :
                        i = new Intent(this, RulesActivity.class);
                        startActivity(i);
                        break;
    case R.id.muteBtn :                 

                        if(IsAudioOn){                                  
                              mute();
                        }
                        else {
                             unmute();
                        }       
    }
}
public void unmute() {
    //for unmute
    amanager = null;
    amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);        
    amanager.setStreamVolume(AudioManager.STREAM_MUSIC, 10, 10);        
    IsAudioOn = true;        
    muteBtn.setBackgroundResource(R.drawable.sound);        
}

public void mute() {
    // for mute
    amanager = null;
    amanager=(AudioManager)getSystemService(Context.AUDIO_SERVICE);        
    amanager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
    IsAudioOn = false;
    muteBtn.setBackgroundResource(R.drawable.nosound);
    }  }

静音和图像的变化似乎正常工作。问题是,当我选择其他按钮中的一个,播放和规则,从而导致其他活动(我之后pressed静音),然后回到StartActivity布尔IsAudioOn再次如此。我怎样才能使活动记住,我把它改成假的?

The mute and the changing of the image seems to work properly. The problem is, when I choose one of the other buttons, play and rules, which lead to other activities (after I have pressed mute) and then go back to the StartActivity the boolean IsAudioOn is again true. How can I make the activity "remember" that I have changed it to false?

感谢您的时间

推荐答案

您必须保存实例状态,因为你的活动可以在后台被破坏并重新创建。在这种情况下,你的布尔isAudioOn将被重置为true。

You have to save instance state because your activity can be destroyed in background and recreated. In this case, your boolean isAudioOn will be reset to true.

对于这一点,重写此方法

For that, override this method

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putBoolean(STATE_IS_AUDIO_ON_KEY, IsAudioOn);
}

您可以检索,然后在你这样的的onCreate 方法该值

You could then retrieve this value in your onCreate method like this

if (savedInstanceState != null) {
    IsAudioOn = savedInstanceState.getBoolean(STATE_IS_AUDIO_ON_KEY, true)
}

详细信息请参见本文档

不要忘了添加密钥不变,例如:

Don't forget to add the key constant, for example

private static final String STATE_IS_AUDIO_ON_KEY = "STATE_IS_AUDIO_ON_KEY";

这篇关于自静音的ImageButton使用布尔不断变化真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆