Android wake lock force close

Tom picture Tom · Jun 14, 2009 · Viewed 7.5k times · Source

Hello I am trying to implement a wake lock for an application.

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

It seems to force close when defining the PowerManager shown in the code above. Whats going wrong?

Answer

snctln picture snctln · Jun 14, 2009

Are you receiving this force close when running the code in the emulator on an actual device?

Have you set breakpoints in eclipse to verify that the getSystemService() call is what is causing the problem?

In eclipse when the force close occurs does LogCat say anything about it?

Did you set

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

in the manifest?

You could also try defining pm as final:

final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);

Let me know if any of this helps...