简体   繁体   中英

Can my AlarmManager fire the first alarm, when the set time is reached, not after the first repeating?

For my app I would like to fire an alarm at midnight and periodically every next midnight to reset some variables. I tried the alarm manager to achieve that. Is it possible, that the alarm fires at the first time it reaches midnight and not just at the following midnights? My actual code is working, but not for I want to achieve. I set the time now and repeating time to 60 seconds for testing.

In my Log outputs I get eg:

  • 20.01.2020_14:15:00 Here I want an ALARM
  • 20.01.2020_14:16:00: ALARM
  • 20.01.2020_14:17:00: ALARM

So is it possible to have an Alarm fired, directly at the first time, when it reaches the set time? Or does someone have a workaround or any tips or hints how I can achieve that?

My function to set the alarm

public void midnightAlarm() {
 AlarmManager alarmMgr;
        PendingIntent alarmIntent;

        alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, MidnightAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);


        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 15);
        calendar.set(Calendar.MINUTE, 15);


        alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                1000 * 60 , alarmIntent);

    }

my Reciever class:

public class MidnightAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

           writeToFile("ALARM");
           //reset my Variables

    }
}

public void midnightAlarm() {

         AlarmManager alarmMgr;
         PendingIntent alarmIntent;

        alarmMgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(this, MidnightAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);


        // Set the alarm to start at 8:30 a.m.
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);



        alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);


    }

Receiver

public class MidnightAlarmReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

           GlobalVariables.writeToFile("ALARM");
           midnightAlarmREDO(context);
    }



    public void midnightAlarmREDO(Context context) {

        AlarmManager alarmMgr;
        PendingIntent alarmIntent;

        alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, MidnightAlarmReceiver.class);
        alarmIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

// zeit neu berechnen

        alarmMgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() +
                        60 * 1000, alarmIntent);


    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM