当前位置:   article > 正文

安卓8.1放弃Java_升级到Android 8.1后,startForeground失败

startforeground 8.1

将手机升级到8.1开发人员预览后,我的后台服务不再正常启动.

在我长期运行的服务中,我实现了一个startForeground方法来启动在create上调用的持续通知.

@TargetApi(Build.VERSION_CODES.O)

private fun startForeground() {

// Safe call, handled by compat lib.

val notificationBuilder = NotificationCompat.Builder(this, DEFAULT_CHANNEL_ID)

val notification = notificationBuilder.setOngoing(true)

.setSmallIcon(R.drawable.ic_launcher_foreground)

.build()

startForeground(101, notification)

}

错误信息:

11-28 11:47:53.349 24704-24704/$PACKAGE_NAMEE/AndroidRuntime: FATAL EXCEPTION: main

Process: $PACKAGE_NAME, PID: 24704

android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification: Notification(channel=My channel pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x42 color=0x00000000 vis=PRIVATE)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1768)

at android.os.Handler.dispatchMessage(Handler.java:106)

at android.os.Looper.loop(Looper.java:164)

at android.app.ActivityThread.main(ActivityThread.java:6494)

at java.lang.reflect.Method.invoke(Native Method)

at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

服务通知的通道无效,显然我的旧通道DEFAULT_CHANNEL_ID不再适用于我假设的API 27.什么是正确的渠道?我试图浏览一下文档

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/580974
推荐阅读
相关标签
  

闽ICP备14008679号