0

My service is a 3-hour timer that should run in the background until the time is finished (even if the user exits the app).

I have successfully created and bound my service with a fragment. This enables me to show the timer in real time to the user.

When I exit the app the service continues properly and even sends a notification to the user once the timer is complete (Even if the app is not open)

My problem is this: When the user closes the app and then re-opens it before the timer is finished, I am unable to retrieve the reference to that running service...

Here is my code to create and bind the service

private ServiceConnection connection = new ServiceConnection(){
    @Override
    public void onServiceConnected(ComponentName componentName, IBinder 
                                   binder){
        TimerService.TimerBinder binderTime
         = (TimerService.TimerBinder) binder;
        Funx.getInstance().setTimerService(binderTime.getOgBinder());
        bound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName){
        bound = false;
    }
};

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

    Intent intent = new Intent(getContext(), TimerService.class);
    Objects.requireNonNull(getContext()).bindService(intent, connection, 
    Context.BIND_AUTO_CREATE);
}

 @Override
    public void onDestroy(){
    super.onDestroy();
    if (bound){
        Objects.requireNonNull(getActivity()).unbindService(connection);
        bound = false;
    }
 }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Paris B. G.
  • 374
  • 1
  • 3
  • 14
  • 2
    you dont need any service - all you need is some database keeping your alarm times and `android.app.AlarmManager` (or [WorkManager](https://developer.android.com/topic/libraries/architecture/workmanager/) support library) – pskink Jan 05 '19 at 16:17
  • 2
    Once you unbind from a Bound Service (and it is the last client) it will be call `onDestroy` so you can't rebind. I assume that by luck the notification works as this is run in a separate `Thread`? By luck Android doesn't kill the App over 3 hours and triggers the notification - only a guess .. – Mark Jan 05 '19 at 16:22
  • So maybe it is better to recreate the service and retrieve the time remaining from a database – Paris B. G. Jan 05 '19 at 16:24
  • By using `WorkManager` (abstraction above other classes like `AlarmManager`) and a database (or even shared preferences) you forgo the need for this current `Service`. – Mark Jan 05 '19 at 16:30
  • lol I saw your comment the first time Mark and thanks. Since I've already constructed my app to use services, I am trying to avoid rewriting all that code, even if you solution is simpler and implemented from a higher level. I will notate this as a last resort, but thanks! – Paris B. G. Jan 05 '19 at 16:34
  • @ParisB.G. What you want isn't how Android works. It can't be done reliably. Use JobScheduler/AlarmManager/WorkManager. – Gabe Sechan Jan 05 '19 at 16:36

0 Answers0