警报器重复设置不起作用的原因是什么?

时间:2013-12-06 06:44:00

标签: android alarmmanager repeat

startSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_START_SLEEP);
startSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, startSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
startSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

endSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_END_SLEEP);
endSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, endSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
endSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

checkWifiIntent = new Intent(CommunicationStrings.ALARAM_CHECK_WIFI_REQUEST);
checkWifiPendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, checkWifiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
checkWifiAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

checkWifiReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (settings.getEnableDisableApp()) {
                // TODO: Check is this a good way to handle threads
                Thread t = new Thread() {
                    @Override
                    public void run() {
                        //Do Some work
                        this.interrupt();
                        // throw new ThreadDeath();
                    }
                };
                t.start();
            //}
        }
    }
};
checkWifiIntetntFilter = new IntentFilter(CommunicationStrings.ALARAM_CHECK_WIFI_REQUEST);
checkWifiIntetntFilter.addAction(CommunicationStrings.ALARAM_END_SLEEP);
checkWifiIntetntFilter.addAction(CommunicationStrings.ALARAM_START_SLEEP);
registerReceiver(checkWifiReceiver, checkWifiIntetntFilter);


private void changeCheckWifiTimerPeriod(long period) {
    checkWifiAlarmManager.cancel(checkWifiPendingIntent);
    checkWifiAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), period, checkWifiPendingIntent);      
}

private void changeStartSleepTimerTime(Calendar alarmTime) {        
    startSleepTimeAlarmManager.cancel(startSleepTimePendingIntent);
    startSleepTimeAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY/*24 * 60 * 60 * 1000*/, startSleepTimePendingIntent);
}

private void changeEndSleepTimerTime(Calendar alarmTime) {
    endSleepTimeAlarmManager.cancel(endSleepTimePendingIntent);
    endSleepTimeAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, alarmTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY, endSleepTimePendingIntent);
}

wifi检查有效,但开始和结束睡眠都不起作用....我查看了有关报警管理器的所有帖子并尝试了所有但我无法弄清楚为什么它不起作用...

可能是什么原因?

编辑:我注意到它只在我第一次运行应用程序时才有效..

2 个答案:

答案 0 :(得分:0)

代码实际上并没有使用AlarmManager启动警报。例如,要获得睡眠警报,您需要执行以下操作:

startSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_START_SLEEP);
startSleepTimePendingIntent =
    PendingIntent.getBroadcast(BackgroundService.this,
                               0,
                               startSleepTimeIntent,
                               PendingIntent.FLAG_UPDATE_CURRENT);

AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP,
        5000, //  Assuming a 5 second wake up
        startSleepTimePendingIntent);

还要记住这些都是一次性警报。因此,在收到待定意图后,您必须使用AlarmManager启动另一个意图,以便在将来再次触发它。

答案 1 :(得分:0)

我找到了解决方案,我为每个警报提供了唯一的requestCode

startSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_START_SLEEP);
startSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 0, startSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
startSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

endSleepTimeIntent = new Intent(CommunicationStrings.ALARAM_END_SLEEP);
endSleepTimePendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 1, endSleepTimeIntent, PendingIntent.FLAG_UPDATE_CURRENT);
endSleepTimeAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);

checkWifiIntent = new Intent(CommunicationStrings.ALARAM_CHECK_WIFI_REQUEST);
checkWifiPendingIntent = PendingIntent.getBroadcast(BackgroundService.this, 2, checkWifiIntent, PendingIntent.FLAG_UPDATE_CURRENT);
checkWifiAlarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);