2

I want to find a reliable way to detect if a user stopped using his phone for a while. When this is the case, I want to show some dialog to the user. I thought about a few methods, but I'm not sure which one is realizable.

  1. If a user did not touch his screen for a while (e.g. 1 minute) and the audio system is inactive*, he most likely stopped using his phone and did something else. In my opinion, this should be quite reliable, but I'm not sure, if it is realizable. Is it?
  2. Almost like method #1, but this time if the screen was off for a while. This should be realizable using Broadcast Receivers (ACTION_SCREEN_OFF: save time; ACTION_SCREEN_ON: check, if now - saved_time > 1 min). Am I correct?
  3. The phone was not moved for a while. This would, however, not cover the cases where the user puts the phone in his pocket. So it would possibly only be reliable in combination with something else.

Do you come across other ways to realize this use case? And do you come across concrete implementation ideas? Which problems do you see in the mentioned methods? Thanks in advance!


*Because otherwise he could listen to music or watch some videos. What about muted videos?

Soham
  • 4,397
  • 11
  • 43
  • 71
flxapps
  • 1,066
  • 1
  • 11
  • 24

2 Answers2

1

I think I would start with the method listed here: Application idle time

This would cover cases of direct user interaction, but as you mentioned it wouldn't cover music/video, so you could write a variation of this as well: How do you check if music is playing by using a broadcast receiver?

Unfortunately, that still leaves your edge case of muted video: That part is a little trickier, because there isn't a broadcast for video playing. You could use your method that you mentioned to check if the screen has been off for a while (no video could be playing), but then you have the edge case where the user has a very long screen timeout, so even when video isn't playing the screen might stay on for 10 minutes.

You can use ActivityManager to see what processes are going on the device, so maybe that could be of use to you? I don't think I've covered everything, but I hope this might be somewhat helpful.

Community
  • 1
  • 1
Patrick Grayson
  • 548
  • 3
  • 17
0

You should have several listeners and receivers.

For example NetworkReceiver which change for connection changes. Validate if a Phonecall has been taken or received (and hooked).

Validate if the Screen was on/off and/or if an app has been installed/removed.

You can also give the app permissions for a DeviceAdmin to check if the lock has been removed/enabled.

You can give the app permissions to hook for key inputs/focuses on the display, etc. (AccessibilityService)

After this has been done you could write using sharedpreferences the last timestamp and reset an AlarmManager.

The AlarmManager should run for the time you want to make sure that the device has been turned off. then you can validate the sharedpreferences if the setting has been changed.

Emanuel
  • 8,027
  • 2
  • 37
  • 56