ForegroundService有时不会立即启动,而是在几分钟后启动Android

wsxa1bj1  于 2022-12-28  发布在  Android
关注(0)|答案(1)|浏览(535)

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

  • 手动禁用了设备电池优化。因此,此foregroundService不会获取ForegroundServiceStartNotAllowedException。此外,也没有ANR或任何崩溃。

报警管理器发送待定Intent

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)

}

大多数情况下,服务会立即启动。但有时它只是暂停,几分钟后,突然启动。
我怀疑wakeLock。然后我也添加了newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "tag")acquire()到这个接收器和服务。但是,行为没有改变。
这是Logcat:服务应该在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

有什么问题吗?任何建议都很感谢。谢谢。

fnvucqvd

fnvucqvd1#

看起来我释放wakeLock的速度比foregroundService启动完成的速度还快。
所以我只在接收器中使用wakeLock acquire(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()
        //}

}

我也在服务端安装了WakeLock。如果有更好的方法,我还是想知道。不管怎样,至少工作起来没有延误。

相关问题