Search code examples
javaandroidserviceandroid-workmanager

Difference between Service and PeriodicWorkRequest()


I have to work on a project Where I have to upload users' locations every 15 minutes. For that, I searched a lot and found Recurring work with PeriodicWorkRequest. But the problem is that the WorkManager might not work when the app is closed/killed per the answer given here. Then I found about Service in android.

So I want to know If I want to send users' locations every 15 min even when the app is killed then how to approach this?


Solution

  • If an application is Force Stopped, the OS cancel all the Job related to that application. This is not a WorkManager only problem. The OS interprets a Force Stop as an user request to the OS that they don't want this application to run anymore.

    Even if you use JobScheduler or a Service, the application is gone. But a force stop should be a user decision.

    Some OEMs have implemented in the past some changes to the Android OS so that a swipe out of an application from the launcher was interpreted as a force stop with all the negative effects on scheduled jobs. This is where the problems start.

    WorkManager is this case has implemented some mitigation, but the application cannot do anything if it is force stopped till the user launch it again.

    If have a problem with a specific OEMs, please open an issue on the Android issuetracker as this maybe a CDD violation. Google can contact the OEM and request that they fix the ROM. This is going to take time, in the meanwhile, you can take a look at sites like don't kill my app to understand what are the constraints on a specific device and use a library like autostarter to help the user to navigate to the right setting.