سؤال

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?

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top