How to acess a Chronometer (TextView) from a Service
Calling:
Intent i = new Intent(Timer.this,TimerService.class);
i.putExtra("ms", ms);
startService(i);
code:
public class TimerService extends Service{
CountDownTimer timer;
Chronometer clock;
@Override
public IBinder onBind(Intent intent) {
开发者_如何学C return null;
}
public void setClock (Chronometer cron)
{
this.clock = cron;
}
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
int ms = intent.getIntExtra("ms", 0);
timer = new CountDownTimer(ms,1000){
@Override
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000) % 60 ;
int minutes = (int) ((millisUntilFinished / (1000*60)) % 60);
int hours = (int) ((millisUntilFinished / (1000*60*60)) % 24);
clock.setText( String.format("%02d:%02d:%02d", hours,minutes,seconds));
Log.e("Timer", String.valueOf(millisUntilFinished));
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
}
}.start();
super.onStart(intent, startId);
}
I have a timerservice, and i want to update the Chronmeter(like a TextView) in the onTick() method. I don't know how to get access to that. please help
maybe you can use AsyncTask ? http://developer.android.com/reference/android/os/AsyncTask.html
精彩评论