简体   繁体   English

在Android中,每天在特定时间设置重复警报

[英]Set Repeating Alarm Every Day at Specific time In Android

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. 我每天早上12点设置闹钟。 And Below is the code for BroadCastReciever 以下是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 谢谢

The alarm will only fire immediately if you set the alarm in the past. 如果您过去设置闹钟,闹钟将立即触发。 Eg it is now 10:00h and you want to set an alarm every day at 09:00. 例如现在是10:00h,你想每天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 ) 为了避免这种情况,您必须查看它现在的时间,如果是这种情况,则将警报移动1天...这允许您使用setRepeating方法(比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

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! 希望这可以帮助您解决问题!

try to use this: 试着用这个:

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

You could use something like this: 你可以使用这样的东西:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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