Prakhar
Prakhar

Reputation: 710

AlarmManager not working with time picker

I have a Service.. I have to generate a notification at the time taken from time picker.. It is working with System.currentTimeMillis(), but not if I take the time from the time picker

Here is the time picker which i am showing on click of textView:

 tv.setOnClickListener(new OnClickListener() {
  @Override
 public void onClick(View v) {
  // TODO Auto-generated method stub
  mcurrentTime = Calendar.getInstance();
  int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
  int minute = mcurrentTime.get(Calendar.MINUTE);
  TimePickerDialog mTimePicker;
  mTimePicker = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {
  @Override
 public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
  timePicker.setIs24HourView(true);
  //tv.setText( selectedHour + " : " + selectedMinute);  
  mcurrentTime.set(Calendar.HOUR,selectedHour);
  mcurrentTime.set(Calendar.MINUTE,selectedMinute);
  mcurrentTime.set(Calendar.SECOND,0);
  // HERE I AM GETTING TIME FROM TIMEPICKER
  l= mcurrentTime.getTimeInMillis();
  tv.setText(String.valueOf(l));
  }
 }, hour, minute, true);//Yes 24 hour tim
  mTimePicker.setTitle("Select Time");
  mTimePicker.show();

  }
 });

Here is the code of button click, on this click i am starting service:

 Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
 int randomPIN = (int)(Math.random()*9000)+1000;
 pendingIntent = PendingIntent.getBroadcast(MainActivity.this, randomPIN, myIntent,pendingIntent.FLAG_ONE_SHOT);
 AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
 //HERE I am entering the time from time picker
 alarmManager.set(AlarmManager.RTC, l , pendingIntent);

Upvotes: 1

Views: 1104

Answers (3)

Dhaval Patel
Dhaval Patel

Reputation: 10299

Replace this:

mcurrentTime.set(Calendar.HOUR, selectedHour);

with this:

mcurrentTime.set(Calendar.HOUR_OF_DAY, selectedHour);

Calendar.HOUR is strictly for 12 hours.

As per Documentation

Field number for get and set indicating the hour of the morning or afternoon. HOUR is used for the 12-hour clock (0 - 11). Noon and midnight are represented by 0, not by 12. E.g., at 10:04:15.250 PM the HOUR is 10.

Upvotes: 2

Zsolt Mester
Zsolt Mester

Reputation: 1053

Try setExact instead of set.

There is a paragraph in set javadoc:

Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

Upvotes: 1

BSavaliya
BSavaliya

Reputation: 827

You can use following code for you service call using intent filter.

https://stackoverflow.com/questions/29818271/android-asynctask-with-alarmmanager-und-service/29818497#29818497

Upvotes: -1

Related Questions