How to save scheduled alarm after app was killed by Android or task killer?

后端 未结 2 912
闹比i
闹比i 2020-12-03 10:12

Code that schedules alarm.

    PendingIntent sender = PendingIntent.getBroadcast(this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager am =          


        
2条回答
  •  时光取名叫无心
    2020-12-03 10:54

    Alarm set by alarm manager is not killed when app is closed, how ever when a reboot occurs all alarms are cleared by the os since there is no persistence. So you need to do the persistence.

    • Every Time while setting a alarm save the alarm time.
    • Register a receiver for boot completion.
    • Set the alarm again on reboot.

      public class BootReceiver extends BroadcastReceiver {
      
          @Override
          public void onReceive(Context context, Intent intent) {
              //re register the alarm
         }
      }
      

    Manifest.xml

    
    .......
    
            
                
            
        
    

    You could use SharedPreference to save the time (time at when the alarm should be triggered or time at when it should be triggered next)

    Use that to set a new alarm at the boot receiver.

提交回复
热议问题