为什么我在 Android 14 上执行代码时遇到“在没有类型的情况下启动 FGS”错误?

Hel*_*oCW 10 android kotlin

代码 A 在 Android 13 或更低版本中运行良好,但在 Android 14 中运行时出现错误 A?

我该如何修复它?

代码A

 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 
            )
            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 ))      //It will show text on status bar, even without having user to "pull down" the incoming notification.
            .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

        startForeground(125, notification)
    }
Run Code Online (Sandbox Code Playgroud)

错误A

  android.app.MissingForegroundServiceTypeException: Starting FGS without a type  callerApp=ProcessRecord{8eb0601 12195:com.hicalc.soundrecorder/u0a190} targetSDK=34
                                                                                                        at android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:53)
                                                                                                        at android.app.MissingForegroundServiceTypeException$1.createFromParcel(MissingForegroundServiceTypeException.java:49)
                                                                                                        at android.os.Parcel.readParcelableInternal(Parcel.java:4870)
Run Code Online (Sandbox Code Playgroud)

小智 16

从 Android 14 (sdk 34) 开始,您必须设置前台服务类型。 https://developer.android.com/about/versions/14/changes/fgs-types-required#permission-for-fgs-type

尝试进入前台时检查 SDK 版本:

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
        startForeground(SERVICE_ID, notification)
} else {
        startForeground(SERVICE_ID, notification, 
FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK)
}
Run Code Online (Sandbox Code Playgroud)

并更新清单:

<uses-permission
    android:name="android.permission.FOREGROUND_SERVICE_xxx"
    android:minSdkVersion="34" />


<application ...>
    <service
        android:name=".feature.exerciseplayer.data.service.YourService"
        android:exported="true"
        android:foregroundServiceType="xxx" />
Run Code Online (Sandbox Code Playgroud)

注:替换xxx为适合您的服务类型。