将手机升级到8.1开发者预览版后,我的后台服务无法正常启动.

在我长期运行的服务中,我实现了一个startForeground方法来启动正在进行的通知,该通知在创建时被调用.

@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)

invalid channel for service notification,显然我原来的DEFAULT_CHANNEL_ID频道已经不再适合API27了.正确的渠道是什么?我试着翻阅了文件

推荐答案

在对不同的解决方案进行了一段时间的修补之后,我发现必须在Android 8.1及更高版本中创建一个通知通道.

private fun startForeground() {
    val channelId =
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                createNotificationChannel("my_service", "My Background Service")
            } else {
                // If earlier version channel ID is not used
                // https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#NotificationCompat.Builder(android.content.Context)
                ""
            }

    val notificationBuilder = NotificationCompat.Builder(this, channelId )
    val notification = notificationBuilder.setOngoing(true)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setPriority(PRIORITY_MIN)
            .setCategory(Notification.CATEGORY_SERVICE)
            .build()
    startForeground(101, notification)
}

@RequiresApi(Build.VERSION_CODES.O)
private fun createNotificationChannel(channelId: String, channelName: String): String{
    val chan = NotificationChannel(channelId,
            channelName, NotificationManager.IMPORTANCE_NONE)
    chan.lightColor = Color.BLUE
    chan.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val service = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    service.createNotificationChannel(chan)
    return channelId
}

据我所知,后台服务现在显示为普通通知,用户可以通过取消 Select 通知通道来 Select 不显示.

Update:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

Android相关问答推荐

编写Inbox需要具有匹配兼容性的Kotlin版本

无法理解Kotlin Coroutines and Flows中的J.C.编程行为

将Any强制转换为Integer将从API返回NullPointerException

在卡片上创建圆角底部边框

如何将我的Android应用程序(Kotlin)中的图像分享给其他应用程序?

制作圆形SupportMapFragment

Color.Transparent 和 Color.Unspecified 之间的区别

修复报错 RecyclerView: No adapter attached;跳过布局

如何从 firebase 实时数据库中检索最后一个值

列表更改时,RecyclerView 中的列表不会更新

围绕动态大小的内容包装 Jetpack Compose Row?

在 Jetpack Compose 中使用 .observeAsState() 时,如何在更改 MutableLiveData 的值后开始执行一段代码?

如何像 XML 一样在 Compose Android Studio 中折叠/展开小部件代码区域/区域

java.lang.IllegalArgumentException:与请求匹配的导航目的地 NavDeepLinkRequest

Android Studio xml 预览问题无法初始化编辑器

TextField 溢出和软包装不适用于 Compose 约束布局

Jetpack Compose:对角拆分卡片并将内容放入其中

线圈单元测试 - 如何做到这一点?

compose FontFamily 错误:必须初始化变量

Android全屏AlertDialog