Set Repeating Alarm Every Day at Specific time In Android

寵の児 提交于 2019-12-30 03:12:14

问题


I am using Alarm manager to run alarm at specific time every day. Below is the code

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

      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent intent = new Intent(this, OnAlarmReceive.class);
        PendingIntent pendingIntent =PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
                24*60*60*1000, pendingIntent);

I am Setting alarm at 12AM every day. And Below is the code for BroadCastReciever

@Override
   public void onReceive(Context context, Intent intent)
   {
           System.out.println("Time is 12 Am");
           Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();     
    }

Problem in this code is Alarm is Triggered As soon as i Run the Application Irrespective of time. Any help will be Appreciated. Thank You


回答1:


The alarm will only fire immediately if you set the alarm in the past. E.g. it is now 10:00h and you want to set an alarm every day at 09:00. To avoid this, you have to look what time it is now, and shift the alarm 1 day if that is the case... This allows you to use the setRepeating method (which is more precise than setInexactRepeating)

This fixes the issue:

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

if(Calendar.getInstance().after(calendar)){
    // Move to tomorrow
    calendar.add(Calendar.DATE, 1);
}

//... continue like before by setting the alarm



回答2:


I had the same issue as you and I couldn't get it to work. Plus all the examples I could find were also setting a specific date, not only just a time. This should work for you:

Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);     
AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent); //Repeat every 24 hours

Hope this helps you fix your problem!




回答3:


try to use this:

alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
        AlarmManager.INTERVAL_DAY, intent);



回答4:


You could use something like this:

Calendar cal = Calendar.getInstance();
    if(cal.getTimeInMillis() < System.currentTimeMillis()) {
        cal.add(Calendar.DAY_OF_YEAR, 7);
    }


来源:https://stackoverflow.com/questions/23751294/set-repeating-alarm-every-day-at-specific-time-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!