Android Question Determine if device was sleeping or awake?

JohnC

Expert
Licensed User
Longtime User
If I schedule a timer (ie. startserviceat) for my app, is there a way to determine if the device was sleeping or already awake when the timer triggered?
 

toby

Well-Known Member
Licensed User
Longtime User
What I do for B4Xpage projects
B4XPage project:
Sub B4XPage_Background
    Log("sleeping")
    isSleeping=True  'declare a class global variable: public isSleeping as boolean=False
End Sub
Sub B4XPage_Foreground
    Log("awake")
    isSleeping=False
End Sub

For Default projects:
default projects:
'for each activity
Sub Activity_Pause
    Log("sleeping")
    starter.isSleeping=True  'declare a Starter global variable: public isSleeping as boolean=False
End Sub
Sub Activity_Resume
    Log("awake")
    starter.isSleeping=False
End Sub
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Thank you toby for the code, but it seems that code seems like it will detect if my app was in the background or not. But I want to know if the "device" was sleeping or not when the scheduled event happened.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
The reason I was looking for this ability is if the device was sleeping when the scheduled event occurred, then that means the device is most likely unattended, so my app will not display a confirmation prompt and instead just do a particular operation automatically.

But if the device was already awake, then it suggests the user is looking at the screen, so then I would want to display a dialog/notification to the user when the scheduled event occurs, asking them if they want to do the operation or not.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
As far as I understand, an app in sleeping mode wouldn't do anything, it's like a polar bear in hibernation, therefore you have nothing to worry about.

What's important is whether it's in foreground or background.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
But my app is designed to do an action on a regular scheduled basis, even if the device is sleeping - so it is using the startserviceat function so it will wake the device, if it was sleeping, so that it can do the action even if the device was sleeping.

But I need to know if the device was sleeping or not so I will know to display a prompt or not before doing the action.
 
Upvote 0
Sleep API can be used to check if device is sleeping or not

Kotlin project using Sleep API is here:

In case anyone turns Sleep API into a class or inline java, please do post that in the forum.
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I appreciate the Sleep API you mention, but I am really interested in knowing if the "device" was sleeping, not if the "user" was sleeping ;)
 
Upvote 0
I appreciate the Sleep API you mention, but I am really interested in knowing if the "device" was sleeping, not if the "user" was sleeping ;)
I figured both are same since when the user goes to sleep, the device too is not being used for a long time and is effectively ‘asleep’.
May be ACTION_USER_PRESENT intent is more appropriate ?
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
That looks interesting - I found this:

"Applies to Broadcast Action: Sent when the user is present after device wakes up"

But then found this:

ACTION_USER_PRESENT is broadcast only if there is a lock screen enabled. So, if the lock screen preference is set to none, this intent is never broadcast.

So it looks like if the device is locked, then it is obviously sleeping, and when the user unlocks it, then the user is probably looking at the screen. But this intent is only broadcast at that moment - which will be a different time than the time my action is scheduled.

An example of what I am looking for...

If the phone is asleep in a users pocket when the scheduled time triggers, I would like my app to know that the device was sleeping so that it will know to just run the scheduled action without prompting the user.

But, if the device is awake at the scheduled time, then that suggests the user is currently looking at the screen, so my app would instead pop-up a notification asking the user if they wish to perform the scheduled action.

I very much appreciate the ideas to help me figure this one out :)
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Your idea of using intents might be the answer because I found:

android.intent.action.SCREEN_OFF
android.intent.action.SCREEN_ON

So, if I configure my app to log the last one of these intents received, then I will know if the screen is off and just do the action without a prompt.
 
Upvote 0
Top