3

I m trying to implement wake lock and wifi lock for a service. More about my scenario in this like: Wakelock implementation in a service. I m acquiring during start and releasing when the service gets destroyed. I want to monitor the battery consumption for my project, though I understand that, this approach is not good. But when my phone screen is off, it is not working. I have enabled the option: Keep Wifi on during sleep. As soon as my screen turns off, my service stops.I m using the snippet :

PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Lock");
if(!wakeLock.isHeld())
{
wakeLock.acquire();
}
Log.d(TAG,"Acquired Wake lock successfully");
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "WiFi LockTag");
if(!wifiLock.isHeld())
{
wifiLock.acquire();
}
Log.d(TAG,"Acquired Wifi lock successfully");  

//TO RELEASE

try{
wifiLock.release(); 
}catch(Exception e){
e.printStackTrace();
Log.d(TAG,"Exception while releasing WakeLock :: Can be ignoredd");
}

Any idea of why this is happening ? Thanks in advance!

Community
  • 1
  • 1
user1741274
  • 759
  • 3
  • 13
  • 25
  • I have a device which does some UPNP network activity. Even when I hold a Partial Wake Lock, and Wifi Lock, when screen is off, UPNP is unreliable. When screen is on, UPNP is fine. Some devices perhaps don't honour the wake lock? – barkside Mar 19 '13 at 11:01

1 Answers1

0

to protect screen from being lock use this Spinet

public static void unlockTheScreen(Context context) {
    KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE); 
    KeyguardLock keyguardLock =  keyguardManager.newKeyguardLock("TAG");
    keyguardLock.disableKeyguard();     
}

Add below in your wake lock

  wakeLock = powermanager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP ), "TAG");
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
  • Hi, Thanks for your reply. When I use partial wakelock, the CPU is on and screen is off.This consumes less power than having a full_wakelock. Isnt so? @Usman Kurd – user1741274 Dec 12 '12 at 12:12