gpt4 book ai didi

Android 13 Foreground service is stopping when app is in background with battery Restricted(Android 13前台服务在应用程序处于电池受限的后台时停止)

转载 作者:bug小助手 更新时间:2023-10-25 19:44:51 31 4
gpt4 key购买 nike



In Android 13, Foreground Service is stopped when the application is background and battery is restricted. This happens after a minute or so.

在Android 13中,当应用程序处于后台且电池受限时,前台服务停止。这会在大约一分钟后发生。


As per the Android developer guide the current running services will be stopped and new services will be not launched and below is the link for the same. Please let me know how to keep service running even if the battery is restricted and application is background?

根据Android开发者指南,当前运行的服务将被停止,不会推出新的服务,下面是相同的链接。请让我知道如何保持服务运行,即使电池有限,应用程序是后台的?


https://developer.android.com/topic/performance/background-optimization

Https://developer.android.com/topic/performance/background-optimization


I have modified code as below, but still the foreground service is stopped and removed the notification from the tray.

我已经按如下方式修改了代码,但前台服务仍然停止,并从托盘中删除了通知。


val notification: Notification = ...;
Service.startForeground(notification, FOREGROUND_SERVICE_TYPE_CONNECTED_DEVICE)

<manifest>
...
<service
android:name=".services.TestService"
android:directBootAware="true"
android:enabled="true"
android:exported="false"
android:foregroundServiceType="connectedDevice"
android:stopWithTask="false" />
</manifest>

Starting ForeGroundService

正在启动ForeGround服务


val startIntent = Intent(applicationContext, TestService::class.java)
startIntent.action = ACTION_START_FOREGROUND
ContextCompat.startForegroundService(applicationContext, startIntent)

更多回答

Anyone find solution??

有人找到解决方案了吗??

优秀答案推荐

The notification may be removed because in android 13 there is new permission that required:"POST_NOTIFICATIONS"

通知可能会被删除,因为在Android 13中有一个需要的新权限:“POST_NOTIFICATIONS”


Also when the screen is turned off the phone enter to DOZE mode and sleep/delay functions will not work, so check if your service uses this functions, becuase I think if you will minimized the app its consider in the background but the Foreground Service will continute to work.

此外,当屏幕关闭时,手机进入睡眠模式,睡眠/延迟功能将不起作用,所以检查你的服务是否使用这些功能,因为我认为如果你会最小化应用程序,它会在后台考虑,但前台服务将继续工作。


Also you can use this to schedule work in the lock screen/turned off screen
just create your AlarmReceiver... via inheritance from broadcast receiver

你也可以用它来安排工作在锁定屏幕/关闭屏幕只是创建您的AlarmReceiver.通过从广播接收器继承


private fun setAlarmClock() {
val intent = Intent(requireContext(), AlarmReceiver::class.java)
alarmIntent = PendingIntent.getBroadcast(requireContext(), 0, intent, PendingIntent.FLAG_IMMUTABLE)

val intervalMillis = 5000L

val alarmClockInfo = AlarmManager.AlarmClockInfo(System.currentTimeMillis() + intervalMillis, alarmIntent)
alarmManager.setAlarmClock(alarmClockInfo, alarmIntent)
}

更多回答

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com