Tag: background service

升级到Android 8.1后,startForeground将失败

将手机升级到8.1 Developer Preview后,我的后台服务不能再正常启动。 在我长时间运行的服务中,我实现了一个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 […]