Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 7.0 (Nougat) Doze Mode Stops Web Service

I have an Android foreground service with a WiFi lock that acts as a web service for another local device. Before Doze mode, acquiring a WiFi lock and being a service as needed worked great.

Even with battery optimizations turned off for my app, the phone is still killing the app when the screen is off for a few minutes.

How do I properly alert Android that a service requested by the customer explicitly is being performed in the foreground, and they do not want their phone to go to sleep at this time?

EDIT: This issue still exists in Android 8.0 (Oreo)

like image 301
Justin Avatar asked Sep 29 '16 16:09

Justin


People also ask

Does Internet work in doze mode?

Network access is disabled in doze mode, regardless if your application is ignoring battery optimizations. The only way to wake-up your device from doze mode and to get network access is by sending a high priority Google Cloud Message to your application.

How do I control doze mode on Android?

Testing your app with Doze You can test Doze mode by following these steps: Configure a hardware device or virtual device with an Android 6.0 (API level 23) or higher system image. Connect the device to your development machine and install your app. Run your app and leave it active.

Which of these activities will occur when a device is in doze mode?

Doze simply keeps your phone from burning through the battery while it's asleep on a desk or table or something. An app that goes into Standby loses all network access and all its background sync jobs are suspended.

How do I turn off doze mode?

Tap the Options menu button (the three dots in the upper right), then tap Battery optimization. Tap the downward-pointing arrow, then tap All apps from the menu that appears. Next, tap the PsyMate app for which you want to switch off Doze mode, and in the box that appears, tap Don't optimize.


1 Answers

you can do this way: just as you call your service, you can add a code to keep screen awake. This will avoid automatic lock of the phone. Below is a sample code snippet from the docs.

    public class MainActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //keep screen awake
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
      }

The advantage of this approach is that unlike wake locks, it doesn't require special permission, and the platform correctly manages the user moving between applications, without your app needing to worry about releasing unused resources. In addition, you can use alarm manager in an effective way as described in this SO answer:

like image 161
Rahul Ahuja Avatar answered Oct 18 '22 02:10

Rahul Ahuja