简体   繁体   中英

how to stop a Runnable running on an UIThread

I have a Runnable on a UIThread updating a SeekBar while a MediaPlayer is playing. Yet, when I switch to another activity my application crashes with an exception cause the Runnable keeps on forever even after the MediaPlayer has been destroyed.

This is my code:

public class Guide extends AppCompatActivity implements MediaPlayer.OnPreparedListener {

    Button back;
    private MediaPlayer m_audio_player;
    private Handler m_handler_seek_bar = new Handler();
    private SeekBar m_seek_bar;
    private Runnable m_seek_bar_runnable;

    private void set_up_audio(){
        m_audio_player = MediaPlayer.create(this, g_audio[NativeLib.get_active_landmark()] );
        m_audio_player.setOnPreparedListener(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide);
        implement_back_button();
        set_up_audio();
    }

    @Override
    protected void onStart() {
        super.onStart();
        set_up_seek_bar();
    }

    private void set_up_seek_bar() {
        m_seek_bar = (SeekBar) findViewById(R.id.seek_bar);
        m_seek_bar.setMax(m_audio_player.getDuration());
        m_seek_bar_runnable = new Runnable() {
            @Override
            public void run() {
                if(m_audio_player != null ){
                    m_seek_bar.setProgress(m_audio_player.getCurrentPosition());
                }
                m_handler_seek_bar.postDelayed(this, 1000);
            }
        };
        // update Seekbar on UI thread
        Guide.this.runOnUiThread(m_seek_bar_runnable);
        m_seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) { }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {}
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(m_audio_player != null && fromUser){
                    m_audio_player.seekTo(progress);
                }
            }
        });
    }

    @Override
    protected void onStop() {
        if (debug_mode) Log.d(TAG, "onStop");
        if (m_audio_player.isPlaying()) {
            m_audio_player.stop();
        }
        m_audio_player.release();
        super.onStop();
        findViewById(R.id.activity_guide).removeCallbacks(m_seek_bar_runnable);
    }

    // return button
    private void implement_back_button() {
        final Intent intent = new Intent(/* pointing at some other activity */);
        back = (Button) findViewById(R.id.back_button);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(intent);
            }
        });
    }
}

You need to use:

m_handler_seek_bar.removeCallbacks(m_seek_bar_runnable);

to remove the runnable from handler

@Override
protected void onStop() {
    if (debug_mode) Log.d(TAG, "onStop");
    if (m_audio_player.isPlaying()) {
        m_audio_player.stop();
    }
    m_audio_player.release();
    m_handler_seek_bar.removeCallbacks(m_seek_bar_runnable);
    super.onStop();
}

Alternatively you can override the onPause() method of the activity:

 @Override
protected void onPause() {
     m_handler_seek_bar.removeCallbacks(m_seek_bar_runnable);
     super.onPause();
}

使用handler.removeCallbacks从onStop中的Handler中删除所有回调

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM