Search code examples
androidbackground-servicechronometer

Chronometer keep in background time spent


i would developing a feature in my app that when click on button on my activity launch a service that start,pause and resume a Chronometer. But I have a problem how start and stop in my background service.

I created my Activity

public class StartWorkActivity extends ActivityGeneralToolbar {

    protected final void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState, R.layout.activity_start_work);
        Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer);



    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    public void startWork(View v){
        Intent msgIntent = new Intent(StartWorkActivity.this, WorkTimerService.class);


        msgIntent.setAction("START_TIMER");
        getBaseContext().startService(msgIntent);
    }

    public void pauseWork(View v){
        Intent msgIntent = new Intent(StartWorkActivity.this, WorkTimerService.class);
        msgIntent.setAction("PAUSE_TIMER");

    }

    public void resumeWork(View v){
       //call service
        Intent msgIntent = new Intent(StartWorkActivity.this, WorkTimerService.class);
        msgIntent.setAction("RESUME_TIMER");


    }


    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onResume() {

        super.onResume();
    }
}

And my WorkTimerService

public class WorkTimerService extends IntentService {

    long timeWhenStopped = 0;
    Chronometer chronometer;

    public WorkTimerService() {
        super("SystemService");

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if(intent.getAction() == "START_TIMER"){
            startWork();
        }
        if(intent.getAction() == "PAUSE_TIMER"){
            pauseWork();

        }if(intent.getAction() == "RESUME_TIMER"){
            resumeWork();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        super.onStartCommand(intent, flags,startId);

        return START_STICKY;
    }
    public void pauseWork(){

        timeWhenStopped = chronometer.getBase() -  SystemClock.elapsedRealtime();
        chronometer.stop();
    }

    public void resumeWork(){

        chronometer.setBase(SystemClock.elapsedRealtime() + timeWhenStopped);
        timeWhenStopped = 0;
        chronometer.start();

    }

    public void startWork(){

        chronometer.start();
    }
}

But my problem is that Chronometer obviously is null in my service, because I read that is not possible, in the service, interact with the ui. And so, how i can send, or work with Chronometer in background?


Solution

  • Chronometer is a UI widget (actually a TextView) in Android. So, you can't use it for non-UI purposes. Try to use Timer or CountDownTimer instead.

    See this for an example usage of Timer inside Service: https://stackoverflow.com/a/3819721/5250273