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?