Android: make notification persist across phone re

2019-01-15 16:29发布

What's the best way to have a status bar notification persist when the phone is turned off and on again? The only solution I can think of is to create the notification in a Service which starts in response to the BOOT_COMPLETED_ACTION Intent.

1条回答
beautiful°
2楼-- · 2019-01-15 17:12

The only solution I can think of is to create the notification in a Service which starts in response to the BOOT_COMPLETED_ACTION Intent.

For raising a Notification, you can probably get by with just doing it in the BOOT_COMPLETED_ACTION BroadcastReceiver, rather than delegating it to a service. However, I agree, this is the only way to do it AFAIK.

Just be sure you do not irritate your users by doing this. Most people expect a relatively clean slate when they reboot their phone. Android assumes that notifications are no longer relevant with a reboot, which is why they do not persist.

So, for example, suppose you were writing an email client, and you use notifications to let the user know about unread messages. The answer should not be "redisplay the unread-message notification after a reboot". The answer should be "check for unread messages after a reboot, and raise the notification if there are unread messages". This way, if there are no unread messages (e.g., user had the phone off for a while and took care of their email on their PC or tablet), they do not get a spurious notification.

查看更多
登录 后发表回答