基本上,我想创建两个警报:
但问题是在执行月度警报之后,第一个警报也在下午4点执行。
我正在创建警报:
Calendar calendar = Calendar.getInstance();
Calendar calendar1 = Calendar.getInstance();
// For 1st alarm
calendar.set(Calendar.HOUR_OF_DAY, 18);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
// For 2nd alarm
calendar1.set(2017,8,17,16,00,00);
Bundle bundle = new Bundle();
Intent intent = new Intent(context, AlarmReceiver.class);
bundle.putInt("NotificationId1", 1);
bundle.putInt("NotificationId2", 2);
intent.putExtras(bundle);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
alarm.set(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), pendingIntent1);
在每月发出警报后,我再次定义警报并进一步确定日期。
问题是在每月发出警报后第一次警报在下午4点执行。如何解决这个问题?
任何形式的帮助都将受到赞赏。
答案 0 :(得分:0)
您的第一个警报正在重复,因此请创建这样的警报以设置警报。
if(Build.VERSION.SDK_INT < 23){
if(Build.VERSION.SDK_INT >= 19){
setExact(...);
}
else{
set(...);
}
}
else{
setExactAndAllowWhileIdle(...);
}
现在在你的广播接收器中设置下一个警报,以便在第二天下午4点开火
答案 1 :(得分:0)
你需要2个意图。 因为您正在共享相同的意图,所以任何运行的警报也会调用相同的意图。
我希望这会有所帮助。
答案 2 :(得分:0)
试试这段代码,希望你能得到答案。
Bundle bundle1 = new Bundle();
Intent intent1 = new Intent(context, AlarmReceiver.class);
bundle1.putInt("NotificationId1", 1);
intent1.putExtras(bundle1);
Bundle bundle2 = new Bundle();
Intent intent2 = new Intent(context, AlarmReceiver.class);
bundle2.putInt("NotificationId2", 2);
intent2.putExtras(bundle2);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 2,
intent2, PendingIntent.FLAG_UPDATE_CURRENT);
答案 3 :(得分:0)
使两个警报彼此分开。为此,您需要2个意图,2个未决意图和2个警报管理器。您可以参考 - this link
你的代码应该像 -
// define variables
private AlarmManager alarmMgr1;
private AlarmManager alarmMgr2;
private PendingIntent pendingIntent1;
private PendingIntent pendingIntent2;
private Calendar timeFirst;
private Calendar timeSecond;
private Context context;
// init variables
timeFirst = Calendar.getInstance();
timeSecond = Calendar.getInstance();
timeFirst.set(Calendar.HOUR_OF_DAY, 18);
timeFirst.set(Calendar.MINUTE, 0);
timeFirst.set(Calendar.SECOND,0);
timeFirst.set(Calendar.MILLISECOND,0);
timeSecond.set(2017,8,17,16,00,00);
// set values
alarmMgr1 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmMgr2 = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent myIntent1 = new Intent(context, AlarmReceiver.class);
pendingIntent1 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent1, PendingIntent.FLAG_UPDATE_CURRENT);
Intent myIntent2 = new Intent(context, AlarmReceiver.class);
pendingIntent2 = PendingIntent.getBroadcast(context, (int) System.currentTimeMillis(), myIntent2, PendingIntent.FLAG_UPDATE_CURRENT);
alarmMgr1.setRepeating(AlarmManager.RTC_WAKEUP, timeFirst.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent1);
if (Build.VERSION.SDK_INT >= 23){
alarmMgr2.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
else if (Build.VERSION.SDK_INT >= 19){
alarmMgr2.setExact(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
else{
alarmMgr2.set(AlarmManager.RTC_WAKEUP, timeSecond.getTimeInMillis(), pendingIntent2);
}
}
希望它会有所帮助并给你一个更好的主意。