postDelayed的用法:
public class MainActivity extends Activity implements OnClickListener{ /** * 音乐播放模块 */ private ImageButton p_w_picpathButton; private ProgressBar progressBar; private TextView textView; private boolean isPlay = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); p_w_picpathButton = (ImageButton) findViewById(R.id.p_w_picpathButton1); progressBar = (ProgressBar) findViewById(R.id.progressBar1); textView = (TextView) findViewById(R.id.textView1); p_w_picpathButton.setOnClickListener(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.p_w_picpathButton1: imgClick(); break; default: break; } } private void imgClick() { isPlay = !isPlay; if(isPlay)//更换播放按钮图片 { p_w_picpathButton.setImageResource(R.drawable.ic_pause); play();//调用播放方法 } else { p_w_picpathButton.setImageResource(R.drawable.ic_play); } } private int playTime; private Runnable action = null; private void play() { new Thread() { public void run() { //使用postDelayed定时发送消息 action = new Runnable()// 创建action时并不执行run()方法 { @Override public void run() { if(isPlay) { playTime++; textView.setText(playTime + ""); p_w_picpathButton.postDelayed(action, 1000);// 回调action progressBar.setProgress(playTime); } } }; p_w_picpathButton.postDelayed(action, 0);// 创建action后执行此方法,回调Runnable()里的run方法 }; }.start(); }}
模拟音乐播放计时和进度条:
public class MainActivity extends Activity implements OnClickListener, OnSeekBarChangeListener{ private ImageButton p_w_picpathButton; private SeekBar seekBar; private TextView textView_playtime; private boolean isPlay = false; private Chronometer mTimer; private TextView textView_lasttime; private long firstClickTime = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); p_w_picpathButton = (ImageButton) findViewById(R.id.p_w_picpathButton1); seekBar = (SeekBar) findViewById(R.id.seekBar1); textView_playtime = (TextView) findViewById(R.id.textView_playtime); textView_lasttime = (TextView) findViewById(R.id.textView_lasttime); mTimer = (Chronometer) findViewById(R.id.chronometer1); p_w_picpathButton.setOnClickListener(this); seekBar.setOnSeekBarChangeListener(this); seekBar.setMax(lastTime()); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.p_w_picpathButton1: imgClick(); break; default: break; } } private void imgClick() { //用户点击间隔在1秒之内,就不响应(不再启动新线程,避免连续点击的加速问题) long secondClickTime = System.currentTimeMillis(); if(secondClickTime - firstClickTime < 1000) { return; } firstClickTime = System.currentTimeMillis(); isPlay = !isPlay; if (isPlay)// 更换播放按钮图片 { p_w_picpathButton.setImageResource(R.drawable.ic_play); play();// 调用播放方法 } else { p_w_picpathButton.setImageResource(R.drawable.ic_pause); } } private int playTime; private Runnable action = null; private void play() // 播放音乐 { new Thread() { public void run() { action = new Runnable() { @Override public void run() { if (isPlay) { if (playTime > lastTime()) { prostop(); } else { textView_playtime.setText(progresstime(playTime)); seekBar.setProgress(playTime); p_w_picpathButton.postDelayed(action, 1000); playTime += 1000; } } } }; p_w_picpathButton.postDelayed(action, 0); }; }.start(); } private void prostop()//播放结束 { new Thread() { @Override public void run() { Runnable actionStop = new Runnable() { @Override public void run() { p_w_picpathButton.setImageResource(R.drawable.ic_pause); isPlay = false; playTime = 0; seekBar.setProgress(playTime); } }; p_w_picpathButton.post(actionStop); } }.start(); } // 得到结束时间的毫秒数 private int lastTime() { String[] str = textView_lasttime.getText().toString().split(":"); return (Integer.parseInt(str[1]) + Integer.parseInt(str[0]) * 60) * 1000; } // 监听进度条方法 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { playTime = progress; } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } // 将毫秒数转换为时间格式 private String progresstime(int progress) { Date date = new Date(progress); SimpleDateFormat format = new SimpleDateFormat("mm:ss"); return format.format(date); } }