How make mobile go sleeping programmatically
Asked Answered
B

1

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?

Byrnie answered 29/11, 2012 at 14:15 Comment(1)
You could take a look at this and see if its of any use: #9561820Landes
H
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

Henze answered 29/11, 2012 at 14:47 Comment(2)
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.Byrnie
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.Henze

© 2022 - 2024 — McMap. All rights reserved.