开发者

Android: Display/update clock in textview

开发者 https://www.devze.com 2023-02-27 11:49 出处:网络
I want to display and update a clock in my app. The time to be displayed is taken via a webservice. Now how do I update the textview each second/minute? And also how do I keep the time synchroniz开发

I want to display and update a clock in my app. The time to be displayed is taken via a webservice.

Now how do I update the textview each second/minute? And also how do I keep the time synchroniz开发者_JAVA百科ed?


Since your time is already taken from a webservice, you should keep taking it at a fixed rate (like every second, minute, etc.) using a Timer with a Service or an AsyncTask implementation.

You can update your TextView's content by

((TextView)findViewById(R.id.myTextView)).setText(formattedTime);

Where

  • R.id.myTextView refers to the id of your TextView defined in your layout xml, and
  • formattedTime is the time that you've got from the web service, and formatted it to be readable.


You could have a thread running and have it sleep the required number of minutes/seconds (after being converted from ms). And then update this text through the thread via a handler that updates it for you. However; one thing you can potentially use is the system clock to do this for you (If they are the same).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号