-1

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;

        }
    };
Ali Ansari
  • 219
  • 3
  • 14
  • have You added the time in milliseconds from a date? What I mean is, have You for example made an Calendar Instance, give time to it and then get the date as milliseconds? How is Your startTimeInMillis calculation? – Opiatefuchs Nov 26 '14 at 12:56
  • 1
    Also for KitKat alarm delivery is inexact. the OS will shift alarms in order to minimize wakeups and battery use. – Kalpesh Patel Nov 26 '14 at 12:59
  • @Opiatefuchs, let me share code for time. – Ali Ansari Nov 26 '14 at 12:59
  • This might be the case, setRepeating is inexact [link](http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating(int,long,long,android.app.PendingIntent). So try to put some larger time period in startTimeInMilis and check. – Chitrang Nov 26 '14 at 12:59
  • @Chitrang, So what u saying is that i should use `setInexactRepeating` instead of `setRepeating`?? – Ali Ansari Nov 26 '14 at 13:07
  • @Ali what time duration you have set? And can you check same with set (int type, long triggerAtMillis, PendingIntent operation) method, please. – Chitrang Nov 26 '14 at 13:07
  • At TimePciker, i am ,say, picking 4.30Pm, and then i m converting it which is ok – Ali Ansari Nov 26 '14 at 13:11
  • @Chitrang, sorry i didnt get you first, I need time interval at daily routine. – Ali Ansari Nov 26 '14 at 13:13
  • 1
    Ok I got your point, you need to add hours to current time, startTimeInMilis = System.currentTimeMillis() + (selectedMinute * 60 + selectedHour * 60 * 60) * 1000; – Chitrang Nov 26 '14 at 13:17
  • 1
    Thnx alot @Chitrang, you reached exactly where i was mistaken. – Ali Ansari Nov 26 '14 at 13:21

1 Answers1

1

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();
    }
};
Kalpesh Patel
  • 1,638
  • 1
  • 20
  • 35