Android 14运行代码时遇'Starting FGS without a type'错误如何解决?
Android 14 前台服务启动报错修复方案
问题描述
原有代码在 Android 13 及更低版本运行正常,但将 targetSDK 设为 34(Android 14)时,启动前台服务会抛出 MissingForegroundServiceTypeException,错误提示为Starting FGS without a type。
错误原因
Android 14 对前台服务新增了强制类型要求:所有前台服务必须明确声明其用途类型,系统会根据类型管控服务权限和行为,未指定类型的服务启动时会触发异常。
修复步骤
1. 在清单文件中声明服务类型
打开AndroidManifest.xml,找到对应的前台服务标签,添加android:foregroundServiceType属性,根据服务实际功能选择合适的类型。示例中应用是录音器,所以选择mediaRecording:
<service android:name=".YourForegroundServiceClass" <!-- 替换为你的服务类名 --> android:foregroundServiceType="mediaRecording" />
可选类型包括mediaPlayback、location、camera、microphone等,需严格匹配服务实际用途。
2. 启动前台服务时指定类型
修改startForegroundService()方法中的startForeground调用,针对 Android 14 及以上版本传入类型参数:
核心修改片段:
val notification = builder.build() notification.flags = notification.flags or NotificationCompat.FLAG_ONGOING_EVENT or NotificationCompat.FLAG_NO_CLEAR // 适配Android 14的前台服务类型 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { startForeground(125, notification, Service.FOREGROUND_SERVICE_TYPE_MEDIA_RECORDING) } else { startForeground(125, notification) }
3. 可选优化:给通知通道关联服务类型
如果需要,可以在创建NotificationChannel时也关联对应类型,让系统更准确识别服务用途:
val notificationChannel = NotificationChannel( CHANNEL_ID, mChannelName, NotificationManager.IMPORTANCE_LOW ) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { notificationChannel.setForegroundServiceType(NotificationManager.FOREGROUND_SERVICE_TYPE_MEDIA_RECORDING) } notificationManager.createNotificationChannel(notificationChannel)
完整修复后的代码
private fun startForegroundService() { val builder = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val mChannelName = getString(R.string.app_name) val notificationManager = this.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationChannel = NotificationChannel( CHANNEL_ID, mChannelName, NotificationManager.IMPORTANCE_LOW ) // 适配Android 14:给通知通道关联服务类型 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { notificationChannel.setForegroundServiceType(NotificationManager.FOREGROUND_SERVICE_TYPE_MEDIA_RECORDING) } notificationManager.createNotificationChannel(notificationChannel) NotificationCompat.Builder(this, notificationChannel.id) } else { NotificationCompat.Builder(this) } val myIntent = Intent(this, ActivityMain::class.java) myIntent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) val pendingIntent = PendingIntent.getActivity( this, 0, myIntent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) builder.setSmallIcon(R.drawable.notify_icon) .setContentTitle(getString(R.string.notificationTitle)) .setTicker(getString(R.string.notificationTicker )) .setContentText(getString(R.string.notificationContent)) .setContentIntent(pendingIntent) val notification = builder.build() notification.flags = notification.flags or NotificationCompat.FLAG_ONGOING_EVENT or NotificationCompat.FLAG_NO_CLEAR // 适配Android 14:启动前台服务时指定类型 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) { startForeground(125, notification, Service.FOREGROUND_SERVICE_TYPE_MEDIA_RECORDING) } else { startForeground(125, notification) } }
内容的提问来源于stack exchange,提问作者HelloCW
相关产品推荐
相关产品推荐

