Always show service in notification bar

后端 未结 4 2063
名媛妹妹
名媛妹妹 2020-12-14 22:45

I want to add my app to the notification bar so that it always shows, like some apps in the Google Play store.

I want it to be like this screen shot:

4条回答
  •  一生所求
    2020-12-14 23:31

    In order to have your notification always present, you'll want to set these two flags:

    notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
    

    Note that while setting your Service to be in the foreground will also get you an ongoing event, that is a very inappropriate thing to do unless you truly do need your Service to run in the foreground. A music player is a good example of an app that should do that -- the user has an expectation that their music will play without interruption, even when doing many other things with the device.

    Most Services, however, can afford to be temporarily stopped by the system when memory is low, and then restarted automatically when memory is available again. So the correct way to think about it is to separate the two ideas.

    1. If you want your notification to always be visible, use the two flags I mentioned.
    2. If you happen to also need your Service to run in the foreground, you can and should call Service.startForeground(), but don't think of this as a way to get an ongoing notification.

提交回复
热议问题