0

I want to send data to the server in the background at every 1 minute.

I have tried sending data to the server by using the Alarm Manager which invokes a service at every 1 minute of an interval. It works for some time and later the HTTP call is failing with timeout error continuously. I suspect the app is entering into doze mode and not able to make any network activity.

public class SyncGPSBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    if (!isMyServiceRunning(SyncGPSService.class, context)) {
        Intent dataBackupService = new Intent(context, SyncGPSService.class);
        startWakefulService(context, dataBackupService);
    }
}

private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}}

I tried showing notification in the service while making HTTP call but the result is same.

What is the best way to solve this problem?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Onkar Kore
  • 75
  • 1
  • 7
  • "showing notification in the service while making HTTP call but the result is same." You won't see any difference just by showing notification. Could you please explain this ? – Nakul Aug 31 '18 at 10:00
  • I am trying to make the service as foreground by showing the notification by that way the app can access the internet and don't go into doze mode – Onkar Kore Aug 31 '18 at 10:06
  • Just by showing notification service won't be a foreground service. You will have to start service by using startForegroundService() – Nakul Aug 31 '18 at 10:12
  • u can debug it to make sure there is Internet in ur Activity, Log the response to see it from outside the app. check https://stackoverflow.com/a/47973013/7569106 – Mohamed Embaby Aug 31 '18 at 10:23
  • What is the best way to make an HTTP request in the background at an interval of 1-5 minute even when the app is closed? – Onkar Kore Aug 31 '18 at 10:33

0 Answers0