重复Alarmmanager在相当/夜间停止

时间:2014-05-02 05:11:54

标签: android

我有一名警报器,每天开3小时,并且工作正常。现在我的问题是我不希望警报员在夜间开火,即第二天晚上9点到9点。

我的代码:

 Intent myIntent = new Intent(context, MyScheduledReceiver.class);
   PendingIntent pendingIntent = PendingIntent.getBroadcast(context,  0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
   //PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
   AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
   Calendar calendar = Calendar.getInstance();
   calendar.setTimeInMillis(System.currentTimeMillis());
   calendar.set(Calendar.MINUTE, 0);
   calendar.set(Calendar.SECOND, 0);
   calendar.set(Calendar.MILLISECOND, 0);
   long interval = 3*60*60*1000;
   alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);

为了阻止此警报管理员重复,我使用此代码段手动停止。

Intent myIntent = new Intent(context, MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);

现在我如何检查时间是在夜间还是在白天?

0 个答案:

没有答案