繁体   English   中英

取消选中“显示通知”后,服务仍在前台运行吗?

[英]Does service still run in foreground after “show notifications” is un-checked?

我知道要作为前台服务运行,您需要显示一条通知。 我的应用程序将服务作为前台服务运行,并显示一条通知。

但是,如果用户进入“设置”->“应用程序”->“我的应用程序”->,并取消选中“显示通知”,该服务是否将继续作为前台服务运行?

我以为是,它将继续作为前台服务运行。

但是,我最近注意到我的服务被系统杀死,并在logcat中看到了这一点:

杀死2647:com.myapp.myapp:Service / u0a122(adj 15):杀死背景

该服务被杀死,并且它本身没有重新启动。 这是在Android 4.4.2上。 最糟糕的是,没有调用onDestroy,所以我什至无法释放一些唤醒锁。

是; 取消选中“显示通知”后,服务仍在前台运行。

运行adb shell dumpsys activity services并检查adb shell dumpsys activity servicesisForeground标志的值。

  * 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

兼容性(使用官方的Android模拟器)

无法运作:

  • 4.0.2(“显示通知”设置不​​可用)

工作方式:

  • 4.1.2
  • 4.2.2
  • 4.3.1
  • 4.4.2
  • 5.0.2
  • 5.1.1
  • 6.0
  • 7.0
  • 7.1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM