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;
}
}