androidandroid-serviceforeground

How to startForeground() without showing notification?


I want to create a service and make it run in the foreground.

Most example codes have notifications on it. But I don't want to show any notification. Is that possible?

Can you give me some examples? Are there any alternatives?

My app service is doing mediaplayer. How to make system not kill my service except the app kill it itself (like pausing or stopping the music by button).


Solution

  • As a security feature of the Android platform, you cannot, under any circumstance, have a foregrounded service without also having a notification. This is because a foregrounded service consumes a heavier amount of resources and is subject to different scheduling constraints (i.e., it doesn't get killed as quickly) than background services, and the user needs to know what's possibly eating their battery. So, don't do this.

    However, it is possible to have a "fake" notification, i.e., you can make a transparent notification icon (iirc). This is extremely disingenuous to your users, and you have no reason to do it, other than killing their battery and thus creating malware.