Is it possible to only show a notification if the device is locked, not when the user is using the device?
Background:
I created an "emergency info" app that shows an ongoing notification with the user's emergency info. I used notifications because they can appear on the lock screen, and the phone is likely to be locked when a first responder tries to use it to identify/help the victim.
Right now, the notification always appears, whether the phone is unlocked or locked. A few users have asked to make it so the notification only appears when the phone is locked, because it's just in the way when they're using their phone normally.
I've tried this so far, in the Starter service:
This seems to almost work - when I lock/unlock the phone, the log shows this event firing. However, it only fires if the app is active or recently used (e.g. still running). If I end the app (e.g. swipe it away in the switcher), the events don't fire.
Is there some way to keep the service running so that I can always detect this event? (I'm not really clear about foreground mode despite reading the posts here.)
Thanks!
Background:
I created an "emergency info" app that shows an ongoing notification with the user's emergency info. I used notifications because they can appear on the lock screen, and the phone is likely to be locked when a first responder tries to use it to identify/help the victim.
Right now, the notification always appears, whether the phone is unlocked or locked. A few users have asked to make it so the notification only appears when the phone is locked, because it's just in the way when they're using their phone normally.
I've tried this so far, in the Starter service:
B4X:
Sub Service_Create
Dim pe As PhoneEvents
pe.Initialize("pe")
End Sub
...
'fires when the user unlocks the phone
Sub pe_UserPresent(Intent As Intent)
LogColor("pe_UserPresent", Colors.magenta)
'add code to hide the notification
End Sub
Sub pe_ScreenOff(Intent As Intent)
LogColor("pe_ScreenOff", Colors.Cyan)
'add code to show the notification
End Sub
This seems to almost work - when I lock/unlock the phone, the log shows this event firing. However, it only fires if the app is active or recently used (e.g. still running). If I end the app (e.g. swipe it away in the switcher), the events don't fire.
Is there some way to keep the service running so that I can always detect this event? (I'm not really clear about foreground mode despite reading the posts here.)
Thanks!
Last edited: