Justin
Justin

Reputation: 3352

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)

Upvotes: 19

Views: 1025

Answers (2)

Rahul Ahuja
Rahul Ahuja

Reputation: 812

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:

Upvotes: 2

rupesh jain
rupesh jain

Reputation: 3430

Try using foreground service-https://developer.android.com/guide/components/services.html#Foreground

A foreground service is a service that the user is actively aware of and is not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the Ongoing heading. This means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground.

You can also try FULL_WAKE_LOCK or SCREEN_DIM_WAKE_LOCK and see if it prevents your the device from going into doze mode.refer -https://developer.android.com/reference/android/os/PowerManager.html#FULL_WAKE_LOCK

Upvotes: 0

Related Questions