S
S
Sergey2019-02-25 21:19:13
Android
Sergey, 2019-02-25 21:19:13

Wake lock not working?

WakeLock not working on android. There is an application - a small server on sockets. Everything works until the screen turns off, after 30 seconds the server becomes unavailable. I found out on the Internet that in order for the processor to work even after turning off the screen, you need to use WakeLock, but for some reason this method does not work for me. I did this:

PowerManager pm = (PowerManager) this.getSystemService(this.POWER_SERVICE);
wl = pow.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "lock");

And in the manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />

I initially stuffed this code into the onCreate activity, and on clicking the button I did wl.acquire(); but that didn't help at all. Further, I tried to transfer it to the service and to the class that inherits Application , but nothing helps. I also checked for an exception, this code does not throw any errors.
Also, in the settings of my smartphone, I disabled the Doze mod for this application, although it stops working faster than this mod should turn on.
The notification was also made in this way:
Notification notif;
@Override
public void onCreate() {
    Notification.Builder builder = new Notification.Builder(this).setSmallIcon(R.drawable.ic_launcher);
    if (Build.VERSION.SDK_INT < 16) {
        notif = builder.getNotification();
    } else {
        notif = builder.build();
    }
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    startForeground(777, notif);
}

What else to do to make the application work in sleep mode?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question