gpt4 book ai didi

android - 无法使用处理程序每​​秒更新 TextView

转载 作者:行者123 更新时间:2023-11-29 14:08:24 25 4
gpt4 key购买 nike

我试图每秒更新一次 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com