Android:保存屏幕状态(初学者)

标签 android session audio media-player screens

我的应用程序遇到了一个问题,我有一个“选项”类,其中有一个 ToggleButton,用于处理我在 session 中创建的音乐播放(打开或关闭)。但是,如果我关闭音乐,切换屏幕并返回选项类,ToggleButton 会返回到“on”,即使音乐不再播放,这意味着我必须按两次按钮才能播放音乐实际上回来了。有人有什么想法吗?

服务等级:

public class MyMusicService extends Service {

MediaPlayer mp;


@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    mp = MediaPlayer.create(this, R.raw.song);


    mp.start();
    mp.setLooping(true);
    return super.onStartCommand(intent, flags, startId);
}


@Override
public IBinder onBind(Intent intent) {

    return null;
}


@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    if(mp!=null) {
        mp.stop();
        mp.release();
    }
    mp=null;
}

选项类:

公共(public)类 OptionsActivity 扩展 Activity {

Intent i; // Handles MyMusicService.java
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.options);
    //setting the layout

    i=new Intent(this, MyMusicService.class);
    final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);

    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 {   
                stopService(i);
                Toast.makeText(OptionsActivity.this, "Music off.", Toast.LENGTH_SHORT).show(); 
            }  


        }});

}

MainActvitiy 类

Intent i;

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    //setting the layout

    //Auto starts playing music on app launch.
    i = new Intent(this,MyMusicService.class);
    startService(i);

这是否与我拥有的 XML 中的事实有关:

android:checked="true"

感谢您的帮助。

最佳答案

是的,ToggleButton 将在您返回此 View 时重新初始化,因此 android:checked="true" 重新发挥作用。

您可以使用方法 myToggleButton.setChecked(boolean isMusicPlaying) 重置按钮的状态。 因此,当您返回此 View 并相应地设置按钮的状态时,您必须检查音乐是否正在播放。


您可以使用 method as described in another thread查看您的音乐服务是否正在运行。

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

因此您只需调用它即可查明您的服务是否正在运行。 然后,在找出 ToggleButton 在布局中的位置后,您可以定义是否应切换它:

boolean musicPlays = isMyServiceRunning(MyMusicService.class.getCanonicalName());
final ToggleButton soundOption = (ToggleButton) findViewById(R.id.soundPref);
soundOption.setChecked(musicPlays)

关于Android:保存屏幕状态(初学者),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9823677/

相关文章:

android - 字体大小变大以适合 Kitkat Only 上的表情符号缓存

android - React-native 无法运行-android

asp.net - 持久化用户 cookie

audio - 为什么使用 `ffmpeg -i input.mp4 -i music.mp3 output.mp4` 向视频添加背景音乐不起作用?

c - 掌握Windows 10的声音管理

.net - 如何响应另一个应用程序中的声音?

java - Android 错误- 工作目录 : null Environment: null

android - Gradle:写故障输出:多余的写4

session - 使用devise重置Rails 3 session

javascript - 如何使用 javascript 读取 flask session ?