3

我有一个叫做BroadcastReceiver“警报工作”的电话。我正在尝试设置重复以在 5:45 和 17:30 触发,但在启动应用程序几秒钟后我看到“警报工作”。为什么会立即发送?AlarmReceiverToastPendingIntentAlarmReceiverPendingIntent

 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();

    }


}

报警接收器:

public class AlarmReceiver extends BroadcastReceiver {


        public void onReceive(Context context, Intent intent) {

            Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();

}
4

2 回答 2

9

但我在我的应用程序启动几秒钟后看到“警报工作”。

我相信你收到第一个警报就好了。

但你相信它不会重复。这个错误,它重复......每~43年一次。您使用的第三个参数setRepeating不正确,请尝试:

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);

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


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

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);
于 2012-12-18T19:47:13.130 回答
3

正如Sam 的帖子所说,您没有使用正确的triggerAtMillisand intervalMillis。我认为您是在告诉它在当天凌晨 5 点触发,这很可能在过去,在这种情况下,文档

Calendar cal1 = Calendar.getInstance();
cal1.add(Calendar.HOUR, 5);
cal1.add(Calendar.MINUTE, 45);

long interval = 17 * 60 * 60 * 1000; // 17 hours converted to milliseconds
interval += 30 * 60 * 1000; // 30 minutes converted to milliseconds

Intent intent = new Intent(this, TestAlarmReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal1.getTimeInMillis(), interval, pi);

可能是您想要做的。

编辑(更多解释):

cal1.add(Calendar.HOUR, 5);获取当前日期和时间并将其增加 5 小时,因此结果是重复PendingIntent将在该日期从当前时间开始 5 小时 45 分钟开始。并且interval表示它将PendingItent从那时起每 17 小时 30 分钟发送第二个和后续的 s(从现在开始是 5 小时 45 分钟)。

如果您想将开始时间设置为上午 5:45,但不希望立即发送(除非此时恰好是上午 5:45)。那么你将要设置做:

cal1.set(Calendar.HOUR, 5);
cal1.set(Calendar.MINUTE, 45);
cal1.set(Calendar.SECOND, 0);

// Current time is after 5:45 am, don't start until tomorrow at 5:45
if (Calendar.getInstance().after(cal1))
    cal1.add(Calendar.DAY_OF_YEAR, 1)

或者,您可以计算从该时间开始的时间间隔,以决定下一次何时触发它,假设它已经在 5:45 开始,但这需要更多的计算工作。

于 2012-12-18T19:57:31.093 回答