前台服务有时不会立即启动,但会在几分钟后启动 Android



我有一个闹钟应用程序。大部分工作正常。然而,在某些设备上(例如:Galaxy Tab A7)与Android 12有时在broadcastReceiver中调用的foregroundService不会立即启动,但在1分钟- 5分钟后突然启动。

*设备电池优化被手动禁用。所以这个foregroundService没有得到ForegroundServiceStartNotAllowedException。也没有ANR或任何崩溃。

alarmmanager发送pendingIntent

alarmMgr?.setExactAndAllowWhileIdle(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
penIntent
)

和AlarmReceiver的onReceive准时接收。并尝试启动ForegroundService.

override fun onReceive(context: Context, intent: Intent) {
Log.d("mytag","going to start foregroundService")
val ringIntent = Intent(context, RingService::class.java)

context.startForegroundService(ringIntent)
}

大多数情况下,Service会立即启动。但有时它只是暂停,几分钟后,突然开始。

我怀疑是wakeLock。然后我还加入了newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag")&acquire()到此接收方和服务。然而,行为根本没有改变。

这是Logcat: Service应该在11:50开始,但是在11:55开始。

12-27 11:50:01.607 20824 20824 D mytag AlarmReceiver: going to start foregroundService
12-27 11:50:01.609  1716  2392 I ActivityManager: Background started FGS: Allowed [callingPackage: com.myapp.myalarm; callingUid: 10569; uidState: RCVR; intent: Intent { cmp=com.myapp.myalarm/.receiver.RingService (has extras) }; code:SYSTEM_ALLOW_LISTED; tempAllowListReason:<,reasonCode:SYSTEM_ALLOW_LISTED,duration:9223372036854775807,callingUid:-1>; targetSdkVersion:33; callerTargetSdkVersion:33; startForegroundCount:0; bindFromPackage:null]
12-27 11:50:01.611  1716  2392 I ActivityManager: Changes in 10569 11 to 4, 0 to 8
12-27 11:50:01.612  2309  2309 D KeyguardUpdateMonitor: handleTimeUpdate
12-27 11:50:01.613  2309  2309 D FaceWidgetPagesController: onTimeChanged()
12-27 11:50:01.613  2309  2309 D FaceWidgetPagesController: refreshViews() reason = ON_TIME_TICK, force = false
12-27 11:50:01.615  1716  1915 D NetworkPolicy: onUidStateChanged() uid: 10569, isForeground(true, 4)
12-27 11:50:01.616  1716  2392 I ActivityManager: Changes in 99145 5 to 4, 8 to 14
12-27 11:50:01.616  1716  2392 I AppOps  : updateUidProcState uid: 99145 procState: 4 capability:14
12-27 11:50:01.616  1716  2392 I AppOps  : commitUidPendingStateLocked() :: UID - 99145, 500 > 400, 8 > 14
12-27 11:50:01.616  1716  1915 D NetworkPolicy: onUidStateChanged() uid: 99145, isForeground(true, 4)
12-27 11:50:01.616  1716  2392 I ActivityManager: Changes in 99144 11 to 4, 0 to 14
12-27 11:50:01.617  1716  2392 I AppOps  : updateUidProcState uid: 99144 procState: 4 capability:14
12-27 11:50:01.617  1716  2392 I AppOps  : commitUidPendingStateLocked() :: UID - 99144, 600 > 400, 0 > 14
12-27 11:50:01.617  1716  2392 I AppOps  : updateUidProcState uid: 10179 procState: 4 capability:15
12-27 11:50:01.617  1716  2392 I AppOps  : commitUidPendingStateLocked() :: UID - 10179, 500 > 400, 15 > 15
12-27 11:50:01.617  1716  1915 D NetworkPolicy: onUidStateChanged() uid: 99144, isForeground(true, 4)
12-27 11:50:01.617  1716  2392 W ProcessStats: Tracking association SourceState{22d18de com.google.android.gms.persistent/10179 BTopFgs #205723} whose proc state 2 is better than process ProcessState{b1dac81 com.google.android.gms/10179 pkg=com.google.android.gms} proc state 3 (6 skipped)
12-27 11:50:01.627   613  4791 I android.system.suspend@1.0-service: !@SystemSuspend: mSuspendCounter-
12-27 11:50:01.627   613  4791 I android.system.suspend@1.0-service: !@SystemSuspend: write mem+
12-27 11:55:01.400  1716  2060 D SamsungAlarmManager: Expired : 65548
12-27 11:55:01.401  1716  2060 V SamsungAlarmManager: Sending to uid : 1000 L=c104bb:TIME_TICK alarm=Alarm{1ccd58c type 3 origWhen 401866093 whenElapsed 401866093 android}
12-27 11:55:01.401  2208  2208 I wpa_supplicant: Heartbeat 10559
12-27 11:55:01.402  2751  2751 I IpcDispatcher<0>: handleMessage: EVENT_SEC_CHANNEL_PROXY_DEAD cookie = 34579 mSecChannelProxyCookie = 34579
12-27 11:55:01.402  1716  2060 D SamsungAlarmManager: setExact (T:3/F:9/AC:false) 20221227T115600 now=402107495 - CU:1000/CP:1716/L:c104bb:TIME_TICK
12-27 11:55:01.402  1716  2060 V SamsungAlarmManager: setLocked to kernel - W:402758646 / NW:402166093, now=402107495
12-27 11:55:01.405   558   558 I hwservicemanager: getTransport: Cannot find entry vendor.samsung.hardware.radio.channel@2.0::ISehChannel/imsd in either framework or device VINTF manifest.
12-27 11:55:01.407  2309  2309 D KeyguardUpdateMonitor: received broadcast android.intent.action.TIME_TICK
12-27 11:55:01.407  2309  2309 D KeyguardUpdateMonitor: handleTimeUpdate
12-27 11:55:01.407  2309  2309 D FaceWidgetPagesController: onTimeChanged()
12-27 11:55:01.408  2309  2309 D FaceWidgetPagesController: refreshViews() reason = ON_TIME_TICK, force = false
12-27 11:55:01.408  2751  2751 E IpcDispatcher<0>: SecChannelProxy getService/setCallback: java.util.NoSuchElementException
12-27 11:55:01.408  2309  2521 D QSClockBellTower: onReceive(android.intent.action.TIME_TICK) mTimeZoneString:null
12-27 11:55:01.411  2309  2309 D QSClockBellTower: He is ready to ring the bell. (((QSClockBellSound - TimeText:11:55, TimeContentDescription:11:55, DateText:Tue, 27 December, ShortDateText:Tue, 27 Dec, Demofalse)))
12-27 11:55:01.416  2309  2309 D QSClockHomeIndicatorView: Home Indicator status_bar_clock notifyTimeChanged(QSClockBellSound - TimeText:11:55, TimeContentDescription:11:55, DateText:Tue, 27 December, ShortDateText:Tue, 27 Dec, Demofalse) ClockVisibleByPolicy:true, ClockVisibleByUser:true, visible?false, parent:android.widget.LinearLayout{6b8e7a5 V.E...... ......I. 0,0-0,36 #7f0b0472 app:id/left_clock_container}
12-27 11:55:01.417  2309  2309 D QSClockBellTower: Everyone heard the bell. run(currentTime:1672109701409,   getTime():Tue Dec 27 11:55:01 GMT+09:00 2022,   getTimeZone():libcore.util.ZoneInfo[mDstSavings=0,mUseDst=false,mDelegate=[id="Asia/Tokyo",mRawOffset=32400000,mEarliestRawOffset=33539000,transitions=9]])
12-27 11:55:01.418   613  4791 I android.system.suspend@1.0-service: !@SystemSuspend: write mem-
12-27 11:55:01.422  1716  2392 I BatteryStatsService: In wakeup_callback: resumed from suspend
12-27 11:55:01.422  1716  2060 V SamsungAlarmManager: Sending to uid : 10569 action=null alarm=Alarm{38fc4d5 type 0 origWhen 1672109700555 whenElapsed 402106647 com.myapp.myalarm}
12-27 11:55:01.425 20824 20824 D mytag RingService: onCreate is called

那么问题是什么呢?任何建议,感谢。谢谢你。

看起来我释放wakeLock的速度比foregroundService启动的速度还快。

所以我只使用wakeLockacquire(timeout)在接收器,它现在看起来工作。

override fun onReceive(context: Context, intent: Intent) {
val wakeLock = (context.getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "flextag::wakeLockStart").apply {
acquire(3000)
}
}
Log.d("mytag","going to start foregroundService")
val ringIntent = Intent(context, RingService::class.java)

context.startForegroundService(ringIntent)

//If release here, this works faster than startForegroundService. So don't implment. 
//if (wakeLock.isHeld()){
//wakeLock.release()
//}
}

我也在Service上实现了WakeLock。如果有更好的办法,我还是想知道。无论如何,至少工作没有延误。

相关内容

  • 没有找到相关文章

最新更新