Search code examples
javaandroidandroid-serviceandroid-wake-lockandroid-1.5-cupcake

Using a wakelock in a service Android 1.5


Hello I am trying to use a service to control a wakelock so I can permanently leave the screen on when my application is running. I create the wakelock and activate it in onCreate() and release it in onDestroy() however I get the error "wl cannot be resolved". Can someone explain how I can get over this? Code below:

public class WakeLockService extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }  
    @Override
    public void onCreate() {
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
        wl.acquire();
    }
    @Override
    public void onDestroy() {
        wl.release();
    }
}

Solution

  • Aren't you missing the line

        PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
    

    in onDestroy()? It is a local variable in onCreate(), but it is not declared in onDestroy() at all.

    Or, more probable, you may want to make it a field of class WakeLockService instead of a local variable.