Question

I know that to run as a foreground service, you need to show a notification. My app runs a service as a foreground service, and it shows a notification.

However, if the user goes into Settings -> Apps- > My app -> and un-checks "show notifications", will the service continue to run as a foreground service?

I assumed that yes, it will continue to run as a foreground service.

However, I recently noticed my service was killed by the system, and saw this in the logcat:

Killing 2647:com.myapp.myapp:Service/u0a122 (adj 15): kill background

The service was killed, and it did not restart itself. This is on Android 4.4.2. Worst of all, onDestroy was not called, so I couldn't even release some wake-locks.

Was it helpful?

Solution

Yes; the service still runs in foreground when "Show notifications" is unticked.

Run adb shell dumpsys activity services and check the value of the isForeground flag for your service.

  * ServiceRecord{e66dea9 u0 com.example.foregroundservice/.ForegroundService}
    intent={cmp=com.example.foregroundservice/.ForegroundService}
    packageName=com.example.foregroundservice
    processName=com.example.foregroundservice:backgroundproc
    baseDir=/data/app/com.example.foregroundservice-2/base.apk
    dataDir=/data/user/0/com.example.foregroundservice
    app=ProcessRecord{254dd51 14363:com.example.foregroundservice:backgroundproc/u0a65}
    isForeground=true foregroundId=101 foregroundNoti=Notification(pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x40 color=0x00000000 vis=PRIVATE)
    createTime=-55s70ms startingBgTimeout=--
    lastActivity=-55s70ms restartTime=-55s70ms createdFromFg=true
    startRequested=true delayedStop=false stopIfKilled=false callStart=true lastStartId=1

Compatibility (using official Android emulators)

Not working:

  • 4.0.2 (the "Show notifications" setting isn't available)

Working:

  • 4.1.2
  • 4.2.2
  • 4.3.1
  • 4.4.2
  • 5.0.2
  • 5.1.1
  • 6.0
  • 7.0
  • 7.1
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top