Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How make mobile go sleeping programmatically

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?

like image 385
Alex Avatar asked Nov 29 '12 14:11

Alex


1 Answers

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

like image 108
nandeesh Avatar answered Sep 24 '22 15:09

nandeesh