I have a following chronometer implementation. I'd like to use it inside Service
then sent its output to my fragment. The problem is that Service
has no findViewById()
because obviously it has no View
at all. Is there any way I can get the chronometer to work inside Service
and if not what can I use instead?
code:
Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);
chronometer.setBase(SystemClock.elapsedRealtime());
chronometer.setOnChronometerTickListener(
new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
// TODO Auto-generated method stub
long myElapsedMillis = SystemClock.elapsedRealtime() - chronometer.getBase();
String strElapsedMillis = String.valueOf(myElapsedMillis);
// Toast.makeText(AndroidChronometer.this, strElapsedMillis, Toast.LENGTH_SHORT).show();
TextView tw5 = (TextView) findViewById(R.id.textView2);
tw5.setText(strElapsedMillis.format("%d min : %d sec",
TimeUnit.MILLISECONDS.toMinutes(myElapsedMillis),
TimeUnit.MILLISECONDS.toSeconds(myElapsedMillis) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(myElapsedMillis))));
}
}
);
chronometer.start();
One option is just to use
to trigger your runnable update method for whatever you update period is, without using a service. And then update the UI in your update method.
Another option is to use AsyncTask instead of Service, if your intention is to do something in the background. AsyncTask has access to the UI on its
onProgressUpdate()
method and you can do your stuff there.If you have to use a Service, then you have to broadcast from the service to the UI activity and let the UI Activity do the view update:
This in your main activity:
In the service class:
Call this method when you want to communicate some "status" to your main activity, like
"Time for update"
or"set chrono to " + x + "milli"
.