0

In an app I'm working on I need to have a timer that tracks the time from the first login until a certain goal is met. For the timer I have been using a Chronometer.

My problem is that the timer will reset if the user force-closes the app from their background apps. I need the timer to persist and update even if the app is force-closed by the user.

Currently, my code looks as follows:

            try{
            FileInputStream fileIn = openFileInput(timerFilename);
            InputStreamReader InputRead = new InputStreamReader(fileIn);

            char[] timeInputBuffer = new char[HomeScreen.READ_BLOCK_SIZE];
            t="";
            int timeCharRead;

            while((timeCharRead = InputRead.read(timeInputBuffer))> 0){
                String readString = String.copyValueOf(timeInputBuffer, 0, timeCharRead);
                t += readString;
            }
            InputRead.close();
        } catch(Exception e){
            e.printStackTrace();
        }

 if(t == null) {
        try {
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.start();
            FileOutputStream fileout = openFileOutput(timerFilename, MODE_PRIVATE);
            OutputStreamWriter outputWriter = new OutputStreamWriter(fileout);
            outputWriter.write(Long.toString(SystemClock.elapsedRealtime()));
            outputWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    else{
        long currentTime = SystemClock.elapsedRealtime() - Long.parseLong(t);

        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"));

        mChronometer.setBase(currentTime);
        mChronometer.setText(sdf.format(new Date(currentTime)));
        mChronometer.start();
    }

I have omitted some unrelated code for brevity.

The main issue is that after supplying the currentTime long to mChronometer.setBase() and then calling mChronometer.start() the timer seems to be completely random and not start where I want it to.

I understand from researching online that calling setBase() and start() will still cause the timer to start counting from 00:00 but I was under the impression that calling mChronometer.setText() should solve that problem, which it is not.

So my question, how can I keep the timer on-time even if the app is forcibly closed?

1 Answers1

0

From developer.android.com

A service is a component that runs in the background to perform long-running operations or to perform work for remote processes. A service does not provide a user interface. For example, a service might play music in the background while the user is in a different application, or it might fetch data over the network without blocking user interaction with an activity. Another component, such as an activity, can start the service and let it run or bind to it in order to interact with it. A service is implemented as a subclass of Service and you can learn more about it in the Services developer guide.

So, as long as you create a service, and the user exits your app it will still run. Just like the example above.

This should give you all the information you need: https://developer.android.com/guide/components/services.html

You can also refer to this for a example on how to implement a countdown timer that runs in a service.


EDIT:

Chronometeris 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

HB.
  • 4,116
  • 4
  • 29
  • 53
  • I've attempted to implement it via a service however I'm still facing the same problem of the timer resetting once the app is force-closed and re-opened. I can upload an updated code snippet if necessary – Woooooooooooooow Jul 06 '17 at 12:55