Hi currently I'm trying to setup a button inside of a my list adapter. The idea of the button is that it grabs a set of values from two edit texts one being hour and the other being minutes values, it then sets it to the calendar and the alarm. After the application reaches a specific time it should then send a notification to the user but currently it sends the notification as soon as the button is pressed instead of waiting for the desired time, can anyone spot my error?
convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom_row_contact_number, null);
alarmManager = (AlarmManager) convertView.getContext().getSystemService(Context.ALARM_SERVICE);
myCalendar = Calendar.getInstance();
year_x = myCalendar.get(Calendar.YEAR);
month_x = myCalendar.get(Calendar.MONTH);
day_x = myCalendar.get(Calendar.DAY_OF_MONTH);
receiver = new Intent(convertView.getContext(), Alarm_Reciever.class);
Button btnSendMessage = (Button)convertView.findViewById(R.id.btnSendMessage);
btnSendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hourValue = Integer.valueOf(editHours.getText().toString());
int minutevalue = Integer.valueOf(editMinutes.getText().toString());
Log.i("hour", Integer.toString(hourValue));
myCalendar.set(Calendar.HOUR_OF_DAY,hourValue);
myCalendar.set(Calendar.MINUTE, minutevalue);
pendingIntent = PendingIntent.getBroadcast(v.getContext(), 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,myCalendar.getTimeInMillis(),pendingIntent);
}
});
}
In addition if I move the below code to oncreate
alarmManager = (AlarmManager) convertView.getContext().getSystemService(Context.ALARM_SERVICE);
myCalendar = Calendar.getInstance();
year_x = myCalendar.get(Calendar.YEAR);
month_x = myCalendar.get(Calendar.MONTH);
day_x = myCalendar.get(Calendar.DAY_OF_MONTH);
receiver = new Intent(convertView.getContext(), Alarm_Reciever.class);
I'm left with the below code but I receive an error
java.lang.NullPointerException: Attempt to invoke virtual method 'void java.util.Calendar.set(int, int)' on a null object reference
Button btnSendMessage = (Button)convertView.findViewById(R.id.btnSendMessage);
btnSendMessage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int hourValue = Integer.valueOf(editHours.getText().toString());
int minutevalue = Integer.valueOf(editMinutes.getText().toString());
Log.i("hour", Integer.toString(hourValue));
myCalendar.set(Calendar.HOUR_OF_DAY,hourValue);
myCalendar.set(Calendar.MINUTE, minutevalue);
pendingIntent = PendingIntent.getBroadcast(v.getContext(), 0, receiver, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP,myCalendar.getTimeInMillis(),pendingIntent);
}
});
It could be because the day is always today, so the scheduled time might be in the past.
This can be solved in a few ways, but an easy one is to add this after you set the hours and minutes.
if(calendar.getTimeInMillis() < System.currentTimeMillis()) {
calendar.add(Calendar.DAY_OF_YEAR, 1);
}