警报管理器2次 [英] alarmmanager 2 times
问题描述
我有一个名为 AlarmReceiver
的 BroadcastReceiver
,它是 Toast
的警报工作。我正在尝试设置重复的 PendingIntent
以在5:45和17:30触发 AlarmReceiver
,但是我在启动应用程序几秒钟后,请参阅警报正常运行。为什么 PendingIntent
立即发送?
I have a BroadcastReceiver
called AlarmReceiver
that Toast
s "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次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!