Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

making a service a foreground service

I have a service that I want to make a Foreground service. My service has no need to communicate to the user. The simplest example I have seen in this forum is to put this code in the service...

    Notification notification = new Notification();
    startForeground(5481, notification);

I have seen a much more complicated example in the Commonsware book pp 606 that uses a Notification Builder (NotificationCompat.Builder). This and other examples have a need to provide complex notifications. I don't have this need.

I understand that, for my software to be acceptable, I must at a minimum display an icon that makes the user aware that my service is running and give the user a way to cancel it. Also If my service decides to stop on it's own, the icon should go away. But that's it. That's all I need to do.

What would I have to add to the above tode to accomplish that? thanks, Gary

like image 947
Dean Blakely Avatar asked Oct 29 '12 21:10

Dean Blakely


1 Answers

Have you read Running a Service in the Foreground?

Your notification needs an icon so that it will show up. The system won't display notifications that don't have icons.

The reasons to use NotificationCompat.Builder are a) it maintains compatibility across platforms and b) with it, it's easier to create simple notifications. You can use Builder to create complex notifications, but you can use it to build simple ones as well.

It's hard to say more without knowing what you're trying to do.

like image 126
Joe Malin Avatar answered Oct 19 '22 16:10

Joe Malin