AlarmManager not working in sleep mode

前端 未结 1 722
北海茫月
北海茫月 2020-12-17 02:11

I have set an AlarmManagr with a repeat time. Here is my method by which I am setting it:

public void setAlarmManager(Context context, Intent intent) {               


        
相关标签:
1条回答
  • 2020-12-17 03:05

    It is because your PendingIntent is calling to a service and not to a BroadcastReceiver which means the device can go back to sleep before your service is even created. If you move to a broadcast receiver it "should" stay awake until the onReceive is complete of the BroadcastReceiver which means you will have time to get a wakelock and start your service. Even if you move to a BroadcastReceiver you will want to acquire a wakelock until you have completed your processing if it's going to take more than 10 seconds (which is the limit of a BroadcastReceiver).

    This is taken from paragraph 2 of the android documentation of AlarmManager:

    "... If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available."

    0 讨论(0)
提交回复
热议问题