Android Question only show notification when locked?

Dave O

Well-Known Member
Licensed User
Longtime User
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:

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:

MarkusR

Well-Known Member
Licensed User
Longtime User
i think if the user drag the app out of the task list then because he not want that something run in the background.
but that would mean the notify did not disappear by your app logik and he need click this notify that this app start and then it can remove this notify.
i would try to remove this notification at UserPresent Event.
in case of an emergency i like to have battery power.
 
Upvote 0
Top