1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question only show notification when locked?

Discussion in 'Android Questions' started by Dave O, Jun 4, 2019.

  1. Dave O

    Dave O Active Member Licensed User

    Is it possible to only show a notification if the device is locked, not when the user is using the device?


    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:

    Sub Service_Create
    Dim pe As PhoneEvents
    End Sub


    'fires when the user unlocks the phone
    Sub pe_UserPresent(Intent As Intent)
    'add code to hide the notification
    End Sub

    Sub pe_ScreenOff(Intent As Intent)
    '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.)

    Last edited: Jun 4, 2019
  2. Erel

    Erel Administrator Staff Member Licensed User

    Yes. With a foreground service. Foreground service = a consistent notification. It will probably not help in this case.

    Run this example to see it: Background location tracking
  3. MarkusR

    MarkusR Well-Known Member Licensed 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.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice