I believe it because itβs an alarm when you call
alarmManager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, interval, pendingIntent);
The variable that you call interval is the time during which you want to skip the next alarm, but when you think about it, when does it know to start? Moreover, when is time actually zero?
When do you create it? Not. When do you call .set() ? Not.
In fact, it is zero on BOOT. Thus, you ask him to run 60 seconds after loading, and your request for this every time, this time has expired.
There is confusion here, and where you probably should just use a call like new Handler.postDelayed(Runnnable r, 60000) instead of the alarm manager. It will be much more accurate and will not be subject to some problems with understanding the Android operating system and its alarms / clock / etc / etc.
But for your specific case, I believe that you can solve it by referring to the calls / variables of the System function. Therefore, inside your setNextScanAlarm() function, I believe it will look like this:
public static void setNextScanAlarm(Context context, int interval) { //create the intent the same way as before Intent scanIntent = new Intent(context, AlarmReceiver.class); scanIntent.putExtra("interval", interval); scanIntent.putExtra("action", "scan"); PendingIntent pendingIntent = PendingIntent.getBroadcast( context, 0, scanIntent, PendingIntent.FLAG_ONE_SHOT); //create new variables to calculate the correct time for this to go off long timeRightNow = System.elapsedRealTime() //use something else if you change AlarmManager type long timeWhenIShouldGoOff = timeRightNow + interval; //use the new timeWhenIShouldGoOff variable instead of interval AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.set( AlarmManager.ELAPSED_REALTIME_WAKEUP, timeWhenIShouldGoOff, pendingIntent); }
napkinsterror
source share