7

I need to do an application that turn off the mobile screen and go to sleep programmatically. For example if screen is on for more than 5 minutes without user_present, I should turn it off. I tried to use PowerManager (goToSleep() function should do exactly what I want, but it seems not working):

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


  if (pm.isScreenOn() )
   {
    pm.goToSleep(System.currentTimeMillis() + 1000
   }

I have the following permission in my manifest:

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

It throws an exception: java.lang.SecurityException: Neither user 10068 nor current process has android.permission.DEVICE_POWER. but I have this permission in my manifest.

Is there another method for doing this, without rooting my phone?

Alex
  • 239
  • 5
  • 12
  • You could take a look at this and see if its of any use: http://stackoverflow.com/questions/9561320/android-how-to-turn-screen-on-and-off-programmatically – Max Nov 29 '12 at 14:36

1 Answers1

3

android.permission.DEVICE_POWER is granted only to system apps, third party apps do not get this permission.

If you are not holding wakelock and if you reduce SCREEN_OFF_TIMEOUT , then you should be able to achieve going to sleep automatically after 5 mins

nandeesh
  • 24,740
  • 6
  • 69
  • 79
  • in fact, that's what application do, if someone hold wakelock without releasing it whithin 5 minutes I should turn off device, to save battery power.By reducing SCREEN_OFF_TIMEOUT I don't know if I'll resolve the problem. – Alex Nov 29 '12 at 14:59
  • If say email app is holding the wakelock, then any third party app should not be able to make phone go to sleep, that would break the functionality, So I dont think that is possible. – nandeesh Nov 29 '12 at 17:12