I also had this problem. After copple of hours, I could point out the problem with this solution. In brief I have used intent extras to notify the alarm time.
in the manifest.xml, register the receiver.
<receiver android:name=".receivers.DayAlarmReceiver"/>
This is how I set the alarm manager
public void setDayAlarm(Context context) {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 4);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent alarmIntent = new Intent(context, DayAlarmReceiver.class);
alarmIntent.putExtra("HOUR_OF_DAY", 4);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 5555, alarmIntent, 0);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("Set the day alarm");
}
This is the OnReceive() method of my DayAlarmReceiver class
@Override
public void onReceive(Context context, Intent intent) {
int setHour = intent.getIntExtra("HOUR_OF_DAY", -1);
Calendar rightNow = Calendar.getInstance();
int currentHour = rightNow.get(Calendar.HOUR_OF_DAY);
if(currentHour == setHour){
Log.d("This is perfect, Trigger the alarm");
showNotification(context, msg);
}else{
Log.d("This is perfect, Trigger the alarm");
}
Happy coding :)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…