Проблема Service.startForeground() в Android 14Android

Форум для тех, кто программирует под Android
Ответить
Anonymous
 Проблема Service.startForeground() в Android 14

Сообщение Anonymous »

Я получаю сообщение об ошибке в целевом SDK версии 34 для Android 14.
android.app.RemoteServiceException$ForegroundServiceDidNotStartInTimeException: Context.startForegroundService() выполнил а затем не вызывать Service.startForeground(): ServiceRecord{5192954 u0 in.test.myApp/.KeepAliveService
в android.app.ActivityThread.generateForegroundServiceDidNotStartInTimeException(ActivityThread.java:2315)
в android.app. ActivityThread.throwRemoteServiceException(ActivityThread.java:2286)
в android.app.ActivityThread.-$$Nest$mthrowRemoteServiceException(Unknown Source:0)
в android.app.ActivityThread$H.handleMessage(ActivityThread. java:2611)
в android.os.Handler.dispatchMessage(Handler.java:106)
в android.os.Looper.loopOnce(Looper.java:230)
в android.os .Looper.loop(Looper.java:319)
в android.app.ActivityThread.main(ActivityThread.java:8913)
в java.lang.reflect.Method.invoke(собственный метод)
в com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
в com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
Вызвано: android.app.StackTrace: последний вызов startServiceCommon() для этой службы был выполнен здесь
в android.app.ContextImpl.startServiceCommon(ContextImpl.java:2023)
в android.app.ContextImpl. startForegroundService(ContextImpl.java:1967)
at android.content.ContextWrapper.startForegroundService(ContextWrapper.java:847)
at androidx.core.content.ContextCompat$Api26Impl.startForegroundService(ContextCompat.java:933)
в androidx.core.content.ContextCompat.startForegroundService(ContextCompat.java:701)
в in.test.myApp.KeepAliveService.onStartCommand(KeepAliveService.kt:53)

Вот мой код
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Util.createNotificationChannel()
val notificationIntent = Intent(this, LauncherActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
this,
0, notificationIntent,
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
)
val notification = NotificationCompat.Builder(this, AppConstant.channelID)
.setContentTitle(AppConstant.Title + " is running")
.setContentText("Tap here to open the app")
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(pendingIntent)
.build()

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
startForeground(1, notification)
} else {
val foregroundServiceType = ServiceInfo.FOREGROUND_SERVICE_TYPE_SHORT_SERVICE

val startIntent = Intent(this, KeepAliveService::class.java)
startIntent.putExtra("notification", notification)
startIntent.putExtra("foregroundServiceType", foregroundServiceType)
ContextCompat.startForegroundService(this, startIntent) // Line number 53

}
return START_STICKY
}


Подробнее здесь: https://stackoverflow.com/questions/781 ... android-14
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Android»