Wake locks on my service and third party programs not working at all

121 views Asked by At

I am making an administrative app for my work, but the problem is that i need to keep the device awake 24/7 so a service can send small UDP packets to my server.

My Alcatel One Touch with android 4.2.2 falls asleep no matter what.

I added a wake lock to my activity and it worked, but after 8 hours or so it seems to freeze the activity and the phone falls asleep.

I moved the wake lock to the service but nothing happens, Wakelock detector detects no wake lock.

I tried using startwakefulservice but the same happens, no wakelock is detected and it falls asleep.

It always falls asleep 20-30 minutes after.

I tried using third party programs, but none of them worked.

What could be the cause?

I rooted my phone, is there any way to change the settings so that the CPU never falls asleep? Anything i could change at all? Can the activity destroying policy be changed?

I tried the third party programs a Motorola XT914 with android 4.1.2 and it does detect it, so i guess its the phone...

The phone will always be plugged and no one will use it, so everything that gets the job done is valid.

A full wake lock seems to work perfectly, but i need the screen to become locked if it cannot turned off...

2

There are 2 answers

0
FunkTheMonk On

Your issue is probably that the service is being destroyed by the system to make resources available for other applications. A Wakelock ensures the device doesn't go to sleep, not that the system won't kill the process if needed. If this is the case, the wakelock is also being released and the only thing you can do (short of a custom Android build) is to make your Service run as a (started (not bound)) Foreground service - making it much less likely that the system will reclaim your Service.

Also, ensure that the Service is "Sticky", and that it will be recreated by the system when possible.

You could also register some broadcast receivers to regular events that re-trigger the service if it has been closed down.

0
Anggrayudi H On

Use this technique to keep the CPU running when your Service is starting:

private PowerManager.WakeLock wakeLock;
...

@Override
public void onCreate() {
    super.onCreate();
    PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakelockTag");
    wakeLock.acquire();
}

@Override
public void onDestroy() {
    wakeLock.release();
    super.onDestroy();
}

Don't forget to add this permission in manifest:

<uses-permission android:name="android.permission.WAKE_LOCK"/>