作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图每秒更新一次 TextView。具体来说,它是一首歌曲的计时器。我正在使用 Handler 来实现它。它似乎设置正确 - 我没有得到任何异常,并且在调试时我没有看到任何明显的错误 - 但它悄悄地无法更新 TextView。它只停留在 0:00。
private TextView currentTime;
private int startTime = 0;
private Handler timeHandler = new Handler();
private Runnable updateTime = new Runnable() {
public void run() {
final int start = startTime;
int millis = appService.getSongPosition() - start;
int seconds = (int) ((millis / 1000) % 60);
int minutes = (int) ((millis / 1000) / 60);
Log.d("seconds",Integer.toString(seconds)); // looks okay, prints new value every second
if (seconds < 10) {
// this is hit, yet the textview is never updated
currentTime.setText(String.format("%d:0%d",minutes,seconds));
} else {
currentTime.setText(String.format("%d:%d",minutes,seconds));
}
timeHandler.postAtTime(this,start+(((minutes*60)+seconds+1)*1000));
}
};
private ServiceConnection onService = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
IBinder rawBinder) {
appService = ((MPService.LocalBinder)rawBinder).getService(); // service that handles the MediaPlayer
// start playing the song, etc...
if (startTime == 0) {
startTime = appService.getSongPosition();
timeHandler.removeCallbacks(updateTime);
timeHandler.postDelayed(updateTime,1000);
}
}
public void onServiceDisconnected(ComponentName classname) {
appService = null;
}
};
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.song);
currentTime = (TextView) findViewById(R.id.current_time);
// ...
bindIntent = new Intent(Song.this,MPService.class);
bindService(bindIntent,onService,0);
}
有什么想法吗?
最佳答案
关于android - 无法使用处理程序每秒更新 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5256762/
程序应每 5 秒在屏幕上打印一次并声明“alive at %d milliseconds\n”。当用户输入时对于投票程序或 对于中断版本,程序应该停止并输出: program terminated b
这是我创建的用于说明问题的示例程序。该程序应该每秒打印(计数)自程序启动(或计数器重置)以来的秒数,并每 10 秒打印一个字符串。每 90 秒,应将不同的字符串写入标准输出。但这并不像预期的那样有效,
我是一名优秀的程序员,十分优秀!