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 yourTextView
defined in your layout xml, andformattedTime
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).
精彩评论