2

In my application, I need to send some data to the server couple of times a day, when a specific event occurred (e.g. Battery drops below 10%)

It works well in the foreground, but in the background, the problems arise, especially on Samsung devices.

I found that after about 8 minutes in the background when trying to make an HTTP request, it always fails due to internet connection unavailability . (It is not a doze mode issue!)

Querying the connectivity manager does approve that there is no connection at that moment.

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
return netInfo != null && netInfo.isConnected(); \\ FALSE IN THIS CASE

I thought about using JobScheduler, but the problem is that the data sending should be no longer than 30~ seconds from the moment the event occurred.

So I tried to use the last resort solution - acquiring wake lock.

PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_DIM_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "WakeLock");
wakeLock.acquire(10000);

It helps, but 50% of the time.

It seems like that also after waking the device for 10 seconds, there is still no internet connection.

I'm all out of ideas.

Is there another solution I didn't think of?

Thanks!

Subin Babu
  • 1,515
  • 2
  • 24
  • 50
dor506
  • 5,246
  • 9
  • 44
  • 79

0 Answers0