2

I'm trying to put a device into sleep mode for a certain amount of time, say x, by calling..

powerManager.goToSleep(xNumberOfMilliseconds);

However, the api never seems to work consistently, and never for any amount of time greater than 1000 milliseconds. I'm stumped. I have the appropriate permissions, my application has its sharedUserId set to "android.uid.system" in the manifest, and the application is signed with the same key the firmware itself is signed with (platform key).

It is a pretty simple API call, so I don't really know what on earth is going wrong. I've been able to get this problem on both a device running android 2.3 and a device running android 3.2.

Any ideas?

Archit
  • 887
  • 2
  • 10
  • 19
  • Please, please, please, how do you managed to call this method and not get exception? There are at least 3 threads on SO on this topic, and all of the sudden you found the way. Please share your way. – greenoldman Oct 05 '12 at 21:18
  • 3
    @macias: See the the question itself. Your app must be signed with the same key as the firmware itself (i.e. the platform key), the sharedUserId (in the manifest) must be set to "android.uid.system", and you must have the appropriate permissions( DEVICE_POWER i think, am not sure, its been a while). What this means for you is that unless you're running your own firmware or are working with the device manufacturer (I was), you cannot call this method. – Archit Oct 08 '12 at 16:43
  • Thank you for clear answer. Since I am just an app developer, it translates for me that this method is unavailable. At least I know where not to pursue any longer. Many thanks again. – greenoldman Oct 08 '12 at 21:46

3 Answers3

1

I have done this but it works at random on several android 4.0.x plaforms.

powerManager.goToSleep(SystemClock.uptimeMillis() + timeMs)

Did anyone managed to use the method the way he has intended to?

Edit: It seems the right answer was what figure in the code below:

public void sleepFor(long time, Context context) {

    //Create a new PendingIntent, to wake-up at the specified time, and add it to the AlarmManager
    Intent intent = new Intent(context, this.getClass());
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent wakeupIntent = PendingIntent.getActivity(context, CODE_WAKE_UP_DEVICE, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    // CODE_WAKE_UP_DEVICE is a dummy request code.

    AlarmManager am = getAlarmManager();
    am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + time, wakeupIntent);

    powerService.goToSleep(SystemClock.uptimeMillis() + 1);
}
slash33
  • 899
  • 1
  • 11
  • 17
  • I'm sorry I dont have access to a system to be able to test this out right now, but how have you checked that the system does indeed sleep?The condition that one is seeking is to have all currently held wakelocks overridden. This is just going to start a new activity after a while, after the sleep time is over. – Archit Aug 27 '12 at 04:39
  • 1
    I have checked power consumption thanks to power supply meter. I am in a lab so I have a wide range of methods to get this measure. It is certain the device goes in deep sleep. During my latest test my code appears to work as expected. I still don't know exactly why I had some lazy wake-ups on previous tests. – slash33 Sep 12 '12 at 09:05
  • The answer here is marked as correct since it is the best/most reasonable answer I've received so far.... Have not been able to fully test it out. – Archit Jan 14 '13 at 16:22
  • For information, the solution is still working on android 4.1.2 – slash33 Jan 16 '13 at 12:23
  • This can't work because `goToSleep` requires a *platform* `DEVICE_POWER` permission (which is not obtainable by normal apps). Source: http://developer.android.com/reference/android/os/PowerManager.html#goToSleep%28long%29 – Saran Jan 28 '13 at 13:13
  • @Saran :Yeah... you need to share the same application ID and the same signing key as the system itself. That is when you are able to use the permission you just mentioned. So you either need your own ROM or you need to be working with the OEM. I was working with the OEM, but the API was not behaving as expected. Hence the question. – Archit Jan 30 '13 at 00:47
  • IMPORTANT: the interface has changed in API level 17: the prototype is the same but the meaning is completely different. Furthermore, there is a new wakeUp(long) method in PowerManager. See details here: http://developer.android.com/reference/android/os/PowerManager.html#wakeUp%28long%29 – slash33 Mar 14 '13 at 18:00
0

ContentResolver cr= getContentResolver();

android.provider.Settings.System.putInt(cr,android.provider.Settings.System.SCREEN_OFF_TIMEOUT ,1000);

Android007
  • 121
  • 1
  • 1
  • 10
  • No, I don't want to just switch off the screen. I need the full functionality of the sleep API, i.e. release all currently held wakelocks and go to sleep for x milliseconds. – Archit Dec 06 '11 at 12:48
0

According to the documentation, current system time would be more appropriate parameter for goToSleep() than the desired sleep duration:

powerManager.goToSleep(SystemClock.uptimeMillis())
Andrei
  • 119
  • 2
  • 1
    The documentation just states that the argument supplied should be in the same base as SystemClock.uptimeMillis() , it does not say that the argument itself should be System.uptimeMillis(). If that were the case then the API could have just supplied the value internally, rendering an argument value moot. – Archit Jan 18 '12 at 11:59