I am trying to do some task at some specific time inserted by user. I have two classes.
1. AddTiminigs
2. TimeReceiver
In OnCreate
of "AddTimings
, I m doing something like this:
manager = (AlarmManager)getActivity().getSystemService(getActivity().ALARM_SERVICE);
Intent alarmIntent_on = new Intent(getActivity(), MuteMeReceiver.class);
alarmIntent_on.putExtra("id", MUTE_ON);
pendingIntent_on = PendingIntent.getBroadcast(getActivity().getApplicationContext(), MUTE_ON, alarmIntent_on, 0);
Then, on some action, i m doing this :
manager.setRepeating(AlarmManager.RTC_WAKEUP, startTimeInMilis,
AlarmManager.INTERVAL_DAY, pendingIntent_on);
And Here is my "TimeReceiver
":
@Override
public void onReceive(Context context, Intent incomingPendingIntent) {
muteStatus = incomingPendingIntent.getIntExtra("id", AddTimeFragment.MUTE_ON);
if(muteStatus == AddTimeFragment.MUTE_ON){
Toast.makeText(context, "GENERAL", Toast.LENGTH_SHORT).show();
}
}
The Problem is that very after adding the time, it doesn't bother what the start time is, it just goes to receiver and not doing the task on the time i mentioned. The timing is in miliseconds, which is ok.
EDIT: I am using TimePicker for starttime. Here is my code:
hour = calendar.get(Calendar.HOUR_OF_DAY);
minute = calendar.get(Calendar.MINUTE);
private TimePickerDialog.OnTimeSetListener listenerForStartTime =new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
if(selectedHour < 12) {
am_pm = "AM";
} else {
am_pm = "PM";
}
startTimeInMilis = (selectedMinute * 60 + selectedHour * 60 * 60) * 1000;
}
};
Change your listenerForStartTime
code as below. You are giving start time wrongly. you should not give remaining time. you should give exact time.
private TimePickerDialog.OnTimeSetListener listenerForStartTime =new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) {
if(selectedHour < 12) {
am_pm = "AM";
} else {
am_pm = "PM";
}
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, selectedHour);
calendar.set(Calendar.MINUTE, selectedMinute);
startTimeInMilis = calendar.getTimeInMillis();
}
};