警报管理器2次

编程入门 行业动态 更新时间:2024-10-23 13:29:03
本文介绍了警报管理器2次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个名为 AlarmReceiver 的 BroadcastReceiver ,它是 Toast 的警报工作。我正在尝试设置重复的 PendingIntent 以在5:45和17:30触发 AlarmReceiver ,但是我在启动应用程序几秒钟后,请参阅警报正常运行。为什么 PendingIntent 立即发送?

I have a BroadcastReceiver called AlarmReceiver that Toasts "alarm worked". I'm trying to set up a repeating PendingIntent to trigger AlarmReceiver at 5:45 and 17:30, but I see "alarm worked" after few seconds of starting the app. Why is the PendingIntent getting sent immediately?

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Calendar cal1 = Calendar.getInstance(); cal1.set(Calendar.HOUR_OF_DAY, 05); cal1.set(Calendar.MINUTE, 45); cal1.set(Calendar.SECOND, 00); Calendar cal2 = Calendar.getInstance(); cal2.set(Calendar.HOUR_OF_DAY, 17); cal2.set(Calendar.MINUTE, 30); cal2.set(Calendar.SECOND, 00); Intent intent = new Intent(this, AlarmReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(),cal2.getTimeInMillis(), pi); Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show(); } }

AlarmReceiver:

AlarmReceiver:

public class AlarmReceiver extends BroadcastReceiver { public void onReceive(Context context, Intent intent) { Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show(); }

推荐答案

,但是启动我的应用几秒钟后,我看到警报起作用。

but I see "alarm worked" after few seconds of starts of my app.

我相信您收到了

但是您相信它不会重复了。这种错误,它将重复...每〜43年一次。您正在使用 setRepeating 错误,请尝试:

But you believe it is not repeating. This wrong, it will repeat... once every ~43 years. You are using the third parameter of setRepeating incorrectly, try:

Calendar cal1 = Calendar.getInstance(); // Now cal1.add(Calendar.SECONDS, 5); // Change to five seconds from now alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), 10000, /* Repeat every 10 seconds */ pi);

第三个参数是 interval 。此代码创建一个警报,警报将在5秒后发出,然后每10秒重复一次。通过使用 cal2 ,您不小心将间隔设置为将近43年。

The third parameter is an interval. This code creates an alarm that goes off in 5 seconds, then repeats every 10 seconds. By using cal2 you are accidentally setting the interval to almost 43 years.

要设置两个不同警报,请使用:

To set two different alarms use:

Calendar cal1 = Calendar.getInstance(); cal1.set(Calendar.HOUR_OF_DAY, 05); cal1.set(Calendar.MINUTE, 45); cal1.set(Calendar.SECOND, 00); Calendar cal2 = Calendar.getInstance(); cal2.set(Calendar.HOUR_OF_DAY, 17); cal2.set(Calendar.MINUTE, 30); cal2.set(Calendar.SECOND, 00); // Test if the times are in the past, if they are add one day Calendar now = Calendar.getInstance(); if(now.after(cal1)) cal1.add(Calendar.HOUR_OF_DAY, 24); if(now.after(cal2)) cal2.add(Calendar.HOUR_OF_DAY, 24); // Create two different PendingIntents, they MUST have different requestCodes Intent intent = new Intent(this, AlarmReceiver.class); PendingIntent morningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0); PendingIntent eveningAlarm = PendingIntent.getBroadcast(getApplicationContext(), 1, intent, 0); // Start both alarms, set to repeat once every day AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, morningAlarm); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), DateUtils.DAY_IN_MILLIS, eveningAlarm);

更多推荐

警报管理器2次

本文发布于:2023-11-27 03:38:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636416.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:管理器   警报

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!