Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Android中每秒更改文本视图_Android - Fatal编程技术网

如何在Android中每秒更改文本视图

如何在Android中每秒更改文本视图,android,Android,我制作了一个简单的Android音乐播放器。我想要一个文本视图,以分:秒格式显示歌曲中的当前时间。因此,我尝试的第一件事是使活动可运行并将其置于run()中: int位置=0; while(MPService.getMP()!=null&&position您必须使用处理程序来处理与GUI的交互。具体地说,线程不能接触主线程上的任何内容。您可以在线程中执行某些操作,如果需要在主线程中更改某些内容,则可以调用处理程序并在那里执行 具体来说,它看起来像这样: Thread t=新线程(new Runn

我制作了一个简单的Android音乐播放器。我想要一个文本视图,以分:秒格式显示歌曲中的当前时间。因此,我尝试的第一件事是使活动可运行并将其置于run()中:

int位置=0;

while(MPService.getMP()!=null&&position您必须使用处理程序来处理与GUI的交互。具体地说,线程不能接触主线程上的任何内容。您可以在线程中执行某些操作,如果需要在主线程中更改某些内容,则可以调用处理程序并在那里执行

具体来说,它看起来像这样:

Thread t=新线程(new Runnable(){
…在这里做事

Handler.postMessage(); }

然后在代码中的其他地方,您可以


处理程序h=新处理程序(){

有些东西。。。 在此处修改ui元素 }


想法是这样的,线程做了一些事情,通知处理程序,然后处理程序接收此消息,并在UI线程上执行类似于更新文本视图的操作。

为此使用
计时器(而不是
while
循环,其中包含
线程。Sleep
)。有关如何使用计时器定期更新UI元素的示例,请参阅本文:

编辑:由于
阿里亚尔多
,更新了回程链接:

编辑2:无回程链接,感谢
gatoatigrado

这个怎么办:

    int delay = 5000; // delay for 5 sec.
    int period = 1000; // repeat every sec.

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
                //your code
            }
        }, delay, period);

这是另一个计时器示例,我正在我的项目中使用此代码。

我认为下面的博客文章显然提供了一个非常好的解决方案。特别是,如果您是一个后台服务,并且希望使用类似计时器的功能定期从该服务更新您的UI。 这对我真的很有帮助,比上面MusiGenesis发布的2007年博客链接多得多


developer.android.com是任何android问题的绝佳起点。:)我知道我检查了你的答案是否正确,但是我在实现这一点上遇到了困难…我没有得到任何异常,它似乎应该正常工作,但它无法无声地设置TextView的文本。如果你想看一看,我已经在那里发布了新代码。我参考了本文并尝试实现,但我使用了mHandler.postDelayed(mUpdateTimeTask,1000);而不是mHandler.posttime(这个,start+((分*60)+秒+5)*1000));我从另一篇博客文章中找到了这个(见下文)这显然提供了一个非常好的解决方案。特别是,如果您是一个后台服务,并且希望使用类似计时器的功能定期从该服务更新您的UI。只是吹毛求疵,线程不能触摸主线程中的任何内容并不完全正确-可以触摸搜索栏。但是,谢谢:)您不能用这种方式从计时器访问UI,它将是未定义的。您需要使用处理程序或扩展TimerTask,以便它接受UI元素作为参数。
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)); // no problem here
        if (seconds < 10) {
            // this is hit, yet the text never changes from the original value of 0:00
            currentTime.setText(String.format("%d:0%d",minutes,seconds));
        } else {
            currentTime.setText(String.format("%d:%d",minutes,seconds));
        }
        timeHandler.postAtTime(this,(((minutes*60)+seconds+1)*1000));
    }

};

private ServiceConnection onService = new ServiceConnection() {
public void onServiceConnected(ComponentName className,
        IBinder rawBinder) {
      appService = ((MPService.LocalBinder)rawBinder).getService();

    // start playing the song, etc. 

    if (startTime == 0) {
        startTime = appService.getSongPosition();
        timeHandler.removeCallbacks(updateTime);
        timeHandler.postDelayed(updateTime,1000);
    }
}
    int delay = 5000; // delay for 5 sec.
    int period = 1000; // repeat every sec.

    Timer timer = new Timer();
    timer.scheduleAtFixedRate(new TimerTask()
        {
            public void run()
            {
                //your code
            }
        }, delay, period);