简体   繁体   中英

controlling the android status bar icon

I am trying to exercise a little control over the state of an icon in the status bar. I want to be able to do the following:

  1. Keep the icon visible in the status bar, as long as the app is running, EVEN IF the user chooses to clear the status bar.
  2. Clear the icon from the status bar if the app is exited, even (especially) if it is killed? I realize I can remove it when the app is exited explicitly, but I want to make sure it goes away if the app is killed. I have to admit I have not tried this yet.

I have not been able to get some good info on this, although I have seen apps that appear to be doing this.

1) Take a look at the developer docs page on status bar notifications . Also note that you'll want to look at the FLAG_NO_CLEAR constant, which should cover your condition.

2) Keeping the icon isn't necessarily a bad thing in the case where the app is killed, and somewhat depends on the purpose of the app. In particular, if your app goes into the background and then gets killed, leaving the icon has actually been noted to be expected behavior by one of Google's engineers :

Correct, onDestroy() is not called when it is killed. This is the same as activity -- the kernel kills processes when needed, not waiting for the app.

The status bar is correctly keeping the icon. The service will later be restarted; it has not been stopped.

It is normal for background services to be killed regularly. This is intentional, because generally background services are not something the user is directly aware of, and restarting their processes every now and then avoids issues with such services consuming increasing amounts of RAM.

If your service is something the user is actually aware of (such as music playback), consider Service.startForeground().

That being said, the icon should probably disappear anyway. Other apps with persistent icons (Meebo comes to mind) will clear away if you kill them with a task manager. I'm not certain if this happens in all cases, though. If your app gets killed while in the background by the OOM, then you most likely won't want to clear it anyway.

  1. http://developer.android.com/reference/android/app/Notification.html#FLAG_NO_CLEAR
  2. The icon is removed automatically (at least when I forcibly kill an app on my phone).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM