AlarmManager not working as expected in sleep mode on S5 Neo

前端 未结 1 2028
南方客
南方客 2021-01-02 13:09

I am using an AlarmManager in a Service to be triggered every minute.

    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0,         


        
相关标签:
1条回答
  • 2021-01-02 13:24

    You should probably use

    AlarmManager#setExact(int type, long triggerAtMillis, PendingIntent operation)

    instead of

    AlarmManager#set(int type, long triggerAtMillis, PendingIntent operation))

    Take a look

    From google :

    AlarmManager#set(int type, long triggerAtMillis, PendingIntent operation))

    Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

    Edit :

    What i am using for an alarm application :

    Manifest :

    <uses-permission android:name="android.permission.WAKE_LOCK"/>

    Java Code :

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
      AlarmManager.AlarmClockInfo alarmClockInfo = new AlarmManager.AlarmClockInfo(nextAlarm.getTimeInMillis(), pendingIntent);
      alarmManager.setAlarmClock(alarmClockInfo, pendingIntent);
    }else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      alarmManager.setExact(android.app.AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pendingIntent);
    }else {
      alarmManager.set(android.app.AlarmManager.RTC_WAKEUP, nextAlarm.getTimeInMillis(), pendingIntent);
    }
    
    0 讨论(0)
提交回复
热议问题