简体   繁体   English

Android:AlarmManager在过去的时间里触发

[英]Android : AlarmManager fires off for past time

This is my code for alarm manager: 这是我的警报管理器代码:

Intent intent=new Intent(getBaseContext(),AlarmReciever.class);
intent.setAction("com.example.projectx.ACTION");

PendingIntent pendingIntent=PendingIntent.getBroadcast(this,12345, intent,PendingIntent.FLAG_CANCEL_CURRENT);

AlarmManager alarmManager=(AlarmManager)getSystemService(Activity.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP,targetCal.getTimeInMillis(),pendingIntent);

The code works great if i select the alarm to fire off at a future hour/minute. 如果我选择在以后的小时/分钟触发警报,该代码将非常有用。 But if I select a past hour/minute it fires up immediately as I click on "set alarm". 但是,如果我选择了过去的一个小时/分钟,则当我单击“设置警报”时,它将立即启动。

Example: 例:

  • now it is 15:00 , i set the alarm for 15:45, alarm goes off at 15:45, everything ok 现在是15:00,我将闹钟设置为15:45,闹钟在15:45响起,一切正常

  • now it is 15:00 , i set the alarm for 14:30, the alarm goes off as soon as i click "set alarm"! 现在是15:00,我将闹钟设置为14:30,当我单击“设置闹钟”时,闹钟就会关闭!

My time picker is always set to 24 hour mode. 我的时间选择器始终设置为24小时模式。 Could that be a problem? 可能有问题吗?

Thank you! 谢谢!

Of course it does... It's meant to work so. 当然可以。它是可以正常工作的。
Android recognizes that the time is past, so it will fire the alarm, even if it's late. Android意识到时间已经过去,因此即使时间已晚,也会发出警报。

You can make sure that the time set for the alarm is after the current time. 您可以确保为闹钟设置的时间在当前时间之后。 Just calculate this difference: 只需计算此差异:

int diff = Calendar.getInstance().getTimeInMilis() - targetCal.getTimeInMillis();

If diff is greater than 0, then add a day to your calendar (targetCal) 如果diff大于0,则在日历中添加一天(targetCal)
Now, your device's time will be earlier (instead of being later ) than the next scheduled alarm time. 现在,您设备的时间将比下一个计划的闹钟时间 (而不是 )。

I think you already found a Solution for your problem but I would like to share a concrete example with code, so that those who have the same problem and read this post, could have it a little bit easier. 我认为您已经找到了解决问题的方法,但是我想与代码共享一个具体的示例,以便那些遇到相同问题并阅读此文章的人可以轻松一些。

    AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(MainActivity.this.ALARM_SERVICE);
    Intent startServiceIntent = new Intent(MainActivity.this,Hintergrundservice.class);
    PendingIntent startServicePendingIntent = PendingIntent.getBroadcast(MainActivity.this,0,startServiceIntent,0);
    Calendar caldendar = Calendar.getInstance();
    caldendar.setTimeInMillis(System.currentTimeMillis());
    caldendar.set(Calendar.HOUR_OF_DAY,7);
    caldendar.set(Calendar.MINUTE,1);
    //long TimeDiffSL = caldendar.getTimeInMillis() - System.currentTimeMillis();
    long TimeUntilTrigger;
    if (System.currentTimeMillis() > caldendar.getTimeInMillis()){
        TimeUntilTrigger = caldendar.getTimeInMillis() + 86400000;
        Toast.makeText(getApplicationContext(),"Morgen erst",Toast.LENGTH_LONG).show();
    }else{
        TimeUntilTrigger = caldendar.getTimeInMillis();
        Toast.makeText(getApplicationContext(),"Heute noch",Toast.LENGTH_LONG).show();
    }

    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,TimeUntilTrigger,AlarmManager.INTERVAL_DAY,startServicePendingIntent);

So the aim is to call the service Hintergrundservice.class every day at 7:01am. 因此,目标是每天早上7:01调用服务Hintergrundservice.class

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

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