如何从服务访问计时器(TextView)

时间:2011-10-04 20:20:02

标签: android

通话:

            Intent i  = new Intent(Timer.this,TimerService.class);
            i.putExtra("ms", ms);
            startService(i);

代码:

public class TimerService extends Service{

    CountDownTimer timer;
    Chronometer clock;
    @Override
    public IBinder onBind(Intent intent) {

        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);
    }

我有一个timerservice,我想在onTick()方法中更新Chronmeter(如TextView)。我不知道如何访问它。请帮忙

1 个答案:

答案 0 :(得分:0)