Archit
Archit

Reputation: 887

Trouble with PowerManager.goToSleep()

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?

Upvotes: 2

Views: 11794

Answers (3)

slash33
slash33

Reputation: 899

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);
}

Upvotes: 1

Andrei
Andrei

Reputation: 119

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

powerManager.goToSleep(SystemClock.uptimeMillis())

Upvotes: 0

Android007
Android007

Reputation: 121

ContentResolver cr= getContentResolver();

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

Upvotes: 0

Related Questions