1

I implemented a Volley Request interval to fire a request periodically:

    final Handler handler = new Handler();

    final int interval = 3000;

    handler.postDelayed(new Runnable() {
        public void run() {

            // Volley request here...
            Volleyclient client = Volleyclient.getInstance();
            client.doSomeVolleyRequest(MainActivity.this, someListener, someDataToSend);

            handler.postDelayed(this, interval);
        }
    }, interval);

It works fine as long as the app is in foreground. If i push the home Button and the app goes asleep i got a Volley TimeoutError and I cannot figure out why. Same behaviour if i switch to "standby" mode (screen off). As soon as i toggle my app back to foreground it continues the volley requests.

I suspect it isnt really a timeout but an internet connection permission issue while falling asleep. I use the following permisions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Is anybody out there who is able to explain the reason for that behaviour? It would be nice if you can show me a workaround to make my app continue sending requests whenever the app is running.

steven
  • 4,868
  • 2
  • 28
  • 58

2 Answers2

2

I think the problem is with your thread. postDelayed operations in going queue in UI threads. When the phone going sleep there is no UI thread for working. I assume to changing thread will solve this problem. Let me know if not works.

Edit: You said your codes in your activity. But when the phone locked(or black screen) all activities going pause state. If want keep working while phone sleeping you have to use service. Services has no lifecycle like activities. They will attached to phone and keep working(even your application has killed) until you make them stop or user interrupt.

There is a example which explains well: How to execute background task when Android app is closed / set to background?

Community
  • 1
  • 1
Eren Utku
  • 1,731
  • 1
  • 18
  • 27
  • Ok, this makes sense. The code is currently placed directly in the onCreate-Method of my MainActivity. Can you point me to a simple solution to put it into another thread by posting a link or a little bit of sample code? – steven Apr 21 '17 at 20:29
  • I migrated everything into a service now. Nearly the same behaviour on real device. If I am connected via USB (debugging) it continues sending if app is not in foreground. If i disconnect the USB it gives me a Volley TimeoutError when not in foreground. In all cases sending failes in sleep mode (display off). Everything works fine if i put the app back to foreground. Very strange and anoying. – steven Apr 21 '17 at 22:20
  • It seems to be the case that the service itself is working fine in background. An Error-Toast ist shown periodically when running in background in all cases so the thread seems to run. Just the volley communication fails. – steven Apr 21 '17 at 22:33
  • Ok, I got it: The reason was the doze mode. Thank you for your advice any way. But the thread wasnt the problem. – steven Apr 23 '17 at 19:20
  • 1
    Great! Happy the hear that! Can you update your question by adding how to solve. – Eren Utku Apr 23 '17 at 21:04
2

After a long time of research I am able to answer my own question now. I hate it but nobody else was able to figure it out.

The simple reason for the descripted behaviour is: The doze mode!

It was not a threading issue it simply was a disabled network access caused by falling into doze mode. One feature of the doze mode is: Network access is suspended.

https://developer.android.com/training/monitoring-device-state/doze-standby.html

https://www.bignerdranch.com/blog/diving-into-doze-mode-for-developers/

steven
  • 4,868
  • 2
  • 28
  • 58