0

I'm trying to figure it out how to do this, I'd like to create a timer from data that comes by server like : 3600 <-- this comes from server, so I'd like to create a timer with this time and have a TextView that is updating it every 1 second and once the timer is done create a dialog, this is not necessary I know how to do it, but I'd like to do like if user put the app in background timer keeps running.

What I thought is to create a BroadcastService and update from there the TextView and if the app goes on background in onPause() I should do something to keep running the timer, and also thought about if timer is done, add it to a SharedPreferences so I can know if the timer is finished or not (if the user had the app in background).

How can I achieve this?

StuartDTO
  • 783
  • 7
  • 26
  • 72
  • If by background you means to recent stack then you can just use a `Handler` or a `Timer` to post each second . Start in `onPause` stop in `onResume` of Application or Activity (whichever the need). – ADM Apr 05 '19 at 10:04
  • Background I mean changing to another app – StuartDTO Apr 05 '19 at 10:16
  • But then when I'm in onResume how do I know if the task is finished or not – StuartDTO Apr 05 '19 at 10:17

2 Answers2

1
Use foreground service we can run countdowntimer that runs in background

Foreground service example Github

satyan_android
  • 364
  • 4
  • 15
  • Please can you help me? I'm using this solution : https://stackoverflow.com/a/42970754/4385913 and even if I put 5 seconds it puts 5 hours... I want just display the exact time, if I send 5 minutes just a timer with 5 minutes – StuartDTO Apr 05 '19 at 10:53
  • i think this is the code you wanna change long int_timer = TimeUnit.HOURS.toMillis(int_hours); to long int_timer = TimeUnit.SECONDS.toMillis(int_hours); – satyan_android Apr 05 '19 at 11:11
1

You can use a Handler to do the periodic sync in your custom Application class. That handler will be destroyed only if your app is killed.

You can extend the Application class to be something like:

public class App extends Application {

    private static final long ONE_MINUTE = 60 * 1000;

    private Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();

        this.handler = new Handler();
        this.handler.postDelayed(syncData, ONE_MINUTE);
    }

    private Runnable syncData = new Runnable() {
        @Override
        public void run() {
            // TODO startSync();
            handler.postDelayed(this, ONE_MINUTE);
        }
    };
}

In that Runnable you can do whatever you want e.g. working with shared preferences.

You can set this custom App class to be your application class in your AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="your_package_name">

    <application
        android:name=".App"
        rest of your code .... >
    </application>

</manifest>
Blehi
  • 1,990
  • 1
  • 18
  • 20
  • Please can you help me? I'm using this solution : https://stackoverflow.com/a/42970754/4385913 and even if I put 5 seconds it puts 5 hours... I want just display the exact time, if I send 5 minutes just a timer with 5 minutes – StuartDTO Apr 05 '19 at 10:53
  • What numbers do you use when you create the new `CountDownTimer()`? – Blehi Apr 05 '19 at 11:08