How do I determine if an android device is idle vs active

db17708

New Member
Licensed User
Longtime User
Hello Everyone,

I am new to B4A and am interested in understanding how to determine if a tablet or phone is currently being used or is inactive.

Although this site is incredibly informative and I have already learned many things from it, I have had no luck googling or searching the B4A forms for a solution and was hoping someone on here could help.

I am writing an App similar to a screen saver where once inactivity is detected, I would like to start a timer and after the timer expires perform some background tasks. But I would also like to be able to stop my background operations if new user activity is detected.
I have familiarized myself with services but my problem has been finding the status/event that will tell me the status of the device.


So my comments/questions are:

1) Since there is a display setting to never turn the screen off, I cannot count on the screen always going into an off state.
2) Shen the device does go to sleep, I still need to continue to perform my background processing.
3) Since it could be a tablet, I cannot count on any phone calls being made.

4) How can I detect if a user is actively using their tablet, versus it's in an idle state(sitting with an App idle, home screen, display off, etc.)?
5) How do I deal with the situation where maybe they are watching a movie so the tablet is active, but there is no user input?

Any help would be greatly appreciated.

db
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Check this tutorial: Android Daydream / DreamService tutorial

When the device is sleeping the screen turns off. If the screen is not off the device is not sleeping.

There is no way to track user inputs when your app is in the background. You can listen to ScreenOn / ScreenOff events with PhoneEvents (Phone library).

As this is a dynamic intent receiver it will only work while your process is running.

See these two tutorials:
Intent Filters - Intercepting SMS messages in the background
Creating a sticky service - long running background tasks
 
Upvote 0

db17708

New Member
Licensed User
Longtime User
Hi Erel,
I was using the phone library, but had a bug in my service code. Now that I fixed it, it works fine for the screen_on, screen_off, and user_present.

Can you tell me is there a reason I should use the above libraries you mention over the phone library?

Thanks
 
Upvote 0
Top