The alarm does not work in android when the device is turned off and on again

I set an alarm to remind me in android that it works when the device is turned on. But when I turn off the device and again on this reminder, the alarm does not work. Can you guys suggest me how to solve this problem?

My code looks like this:

Intent myIntent = new Intent(getApplicationContext(), serviceclass.class); PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), CONST+id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT); Calendar calender = Calendar.getInstance(); calender.setTimeInMillis(System.currentTimeMillis()); calender.set(Calendar.HOUR_OF_DAY, hours); calender.set(Calendar.MINUTE, ireminder.getMin()); calender.set(Calendar.SECOND, 0); calender.set(Calendar.MILLISECOND, 0); calender.set(Calendar.DAY_OF_WEEK, day); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calender.getTimeInMillis(), 7 * AlarmManager.INTERVAL_DAY, pendingIntent); 
+4
source share
2 answers

Upon reboot, alarms will be deleted.

What you can do is

  • save alarm information in db table
  • register for REBOOT_COMPLETED -Event
  • When rebooting, start the background thread, overwrite the alarm. Make sure you calculate the alarm time correctly.

See API: "Registered alarms are saved when the device is sleeping (and if necessary can wake the device if it turns off during this time), but will be cleared if it is turned off and restarted." - http://developer.android.com/reference/android/app/AlarmManager.html

+4
source

Create OnBootReceiver Class

 public class OnBootReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub mgr.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.currentThreadTimeMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pi); } } 

inside the manifest

 <receiver android:name=".OnBootReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> </intent-filter> </receiver> 

inside your business

 sendBroadcast(new Intent(this, OnBootReceiver.class)); 
+1
source

Source: https://habr.com/ru/post/1347485/


All Articles