I have Foreground Service
in my application which sends API request every minute. Here is the code:
public class MyService extends Service {
@Override
public void onCreate() {
Notification notification = getNotification();
int notificationId = 20;
startForeground(notificationId, notification);
Observable.interval(0, 60, TimeUnit.SECONDS)
.doOnNext(aLong -> sendMyRequest())
.subscribe()
}
}
And I have WorkManager
with PeriodicWorkRequest
which starts (or restarts) my service every 15 minutes:
@Override
public Result doWork() {
context.stopService(new Intent(context, MyService.class));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, MyService .class));
} else {
context.startService(new Intent(context, MyService .class));
}
return Result.success();
}
And it works correct when I use the app or when I close the app and have some activity in my device. But when I don't use the device and it is in sleep mode, I see that restarting service works well (every 15 minutes), but app doesn't send requests. As I understand, it happens cause device is in doze mode (OS version is 10) and this mode prevents requests sending. But I really need this functionality (sending request every minute when Foreground Service
works). How can I solve this issue?