我有一个警报,让我们在15点O'Clock说火。我在Boot completed Listener上开始报警,但我想让它每天15:00开火。但是,用户可以修改闹钟时间。问题是,一旦我修改它停止射击的闹钟时间。
以下是我的代码:
public class SystemBootListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"SystemBootListener onReceive", Toast.LENGTH_SHORT).show();
Intent i = new Intent(context, ConnectivityWatcher.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,0);
long _alarm = 0;
Calendar now = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY,Integer.parseInt(AppSettings.getAlarmHour(context)));
int min=Integer.parseInt(AppSettings.getAlarmMin(context));
alarm.set(Calendar.MINUTE,min);
if(alarm.getTimeInMillis() <= now.getTimeInMillis())
_alarm = alarm.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm = alarm.getTimeInMillis();
// Trigger every 10 seconds
// InexactRepeating allows Android to optimize the energy consumption
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, _alarm, 5*1000, pending);
}
public void setAlarm(Context context) {
//Toast.makeText(context,"Alarm Set by Activity"+ Integer.parseInt(AppSettings.getAlarmHour(context)) + ":"+ Integer.parseInt(AppSettings.getAlarmMin(context)),Toast.LENGTH_SHORT).show();
Intent intent = new Intent(context, ConnectivityWatcher.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent,0);
long _alarm = 0;
Calendar now = Calendar.getInstance();
Calendar alarm = Calendar.getInstance();
alarm.set(Calendar.HOUR_OF_DAY,Integer.parseInt(AppSettings.getAlarmHour(context)));
int min=Integer.parseInt(AppSettings.getAlarmMin(context));
alarm.set(Calendar.MINUTE,min);
if(alarm.getTimeInMillis() <= now.getTimeInMillis())
_alarm = alarm.getTimeInMillis() + (AlarmManager.INTERVAL_DAY+1);
else
_alarm = alarm.getTimeInMillis();
// InexactRepeating allows Android to optimize the energy consumption
AlarmManager service = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,_alarm,5*1000, pi);
//service.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), , pi);
Toast.makeText(context,alarm.get(Calendar.HOUR_OF_DAY)+" :"+alarm.get(Calendar.MINUTE), Toast.LENGTH_SHORT).show();
}
public void cancelAlarm(Context context) {
Intent intent = new Intent(context, ConnectivityWatcher.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
}
public void setOnetimeTimer(Context context) {
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, ConnectivityWatcher.class);
//intent.putExtra(ONE_TIME, Boolean.TRUE);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
am.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), pi);
}
}