How to keep awake my android device

360 views Asked by At

Right now my app has to stream music. I'm using a web server and everything works if the phone is awake or is plugged in to a power source.

But I really need to continue streaming until the phone go to sleep. I'm using PowerManager PowerManager.WakeLock in order to keep the phone awake.

This is part of my code:

pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        wl.setReferenceCounted(true);

After that I have the wl.acquire(); and the release. I have a handler that does the release 5 minutes after the songs stop streaming.

int timeToWait = 300000;
turnOffDevice.sendEmptyMessageDelayed(0, timeToWait);

But it works for a short period of time then the song starts jumping and most of time the songs stop.

1

There are 1 answers

0
David On BEST ANSWER

Hi I've already fix my problem, the problem was in the wifi, it was sleeping after some time so I added a wifilock and in the constructor of the class in I'm using WIFI_MODE_FULL_HIGH_PERF this flag avoid the device turn off the wifi

wifilock = manager.createWifiLock(3,TAG);// WIFI_MODE_FULL_HIGH_PERF

The only problem is this flag is only available since API 12, but for now is not a big deal into my project. If you want more information in this linkWIFI_MODE_FULL_HIGH_PERF