开发者

Android foreground service

开发者 https://www.devze.com 2023-04-03 16:34 出处:网络
Here\'s the story: I\'ve created an application that starts at boot time and perform some action at specified interval. To achieve that I\'m creating a service that uses an AlarmManager to create repe

Here's the story: I've created an application that starts at boot time and perform some action at specified interval. To achieve that I'm creating a service that uses an AlarmManager to create repeating event which is then handled by BroadcastReceiver which does the heavy work. When the service is destroyed (user choose to exit the application) the event has to be cancelled, so the service has to keep reference to the same PendingI开发者_运维技巧ntent that was used to start it.

The problem: Service has to call startForeground to ensure it doesn't die so it can maintain the reference to PendingIntent. The notification appears all the time in the status bar and users find it annoying (to be honest, me too).

What next? Ideally I'm looking for solution which will keep the icon in the status bar, but won't display in the drag-down list. This way the user will know it's running and won't be annoyed by waste of space.

Few guesses: Maybe it's possible to restore the reference in service, when it's recreated? If so, how? This way I could avoid using startForeground. But still, how to get the icon into statusbar?

Or maybe someone has a better solution?


First of all you don't have to hold on to PendingIntent all the time. If you have all the required data you can later re-create it to cancel the notification - you just have to re-create it with exactly the same data.

0

精彩评论

暂无评论...
验证码 换一张
取 消