我需要在 30 秒后或任何时候单击按钮时停止 run() 线程。我的问题是如何停止 public void run()。
@Override
public void run() {
// TODO Auto-generated method stub
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
sbMusicProgress.setProgress(currentPosition);
/*MP3 PROGRESS*/
timer_count++;
runOnUiThread(new Runnable() {
public void run() {
if (timer_count<10)
context.txMp3Prog.setText("00:0"+String.valueOf(timer_count));
//Stop playlist after 30seconds
else if (timer_count==30){
timer_count=0;
context.txMp3Prog.setText("00:00");
mp.pause();
sbMusicProgress.setProgress(0);
btPlayMp3.setBackgroundResource(R.drawable.air_deezer_play);
}
else
context.txMp3Prog.setText("00:"+String.valueOf(timer_count));
}
});
}
}