This is my onclick() function.this will set target alarm
SA=(Button)findViewById(R.id.button1); SA.setOnClickListener(new OnClickListener() { @SuppressWarnings("deprecation") @Override public void onClick(View v) { showDialog(id); } }); } @Override protected Dialog onCreateDialog(int id1) { switch (id1) { case id: // set time picker as current time return new TimePickerDialog(this, timePickerListener, hour, min,false); } return null; } private TimePickerDialog.OnTimeSetListener timePickerListener = new TimePickerDialog.OnTimeSetListener() { public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { Calendar calnow=Calendar.getInstance(); calnow.setTimeInMillis(System.currentTimeMillis()); calnow.set(Calendar.HOUR_OF_DAY,selectedHour); calnow.set(Calendar.MINUTE,selectedMinute); calnow.set(Calendar.SECOND, 0); Intent intent=new Intent(getBaseContext(),alarm.class); PendingIntent pendingintent= PendingIntent.getBroadcast(getBaseContext(),0, intent, 0); AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE); alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis(),pendingintent); Toast.makeText(getBaseContext(), "alarm set", Toast.LENGTH_SHORT).show(); } };
public void onReceive(Context arg0, Intent arg1) { AlarmManager mgr = (AlarmManager)arg0.getSystemService(Context.ALARM_SERVICE); Toast.makeText(arg0,"Alarm Started.....", Toast.LENGTH_LONG).show();
Here problem is that I get both toasts "alarm set" and"alarm started" as soon as I click button to set alarm before reaching target alarm.
alarmManager.set(AlarmManager.RTC_WAKEUP, calnow.getTimeInMillis(),pendingintent);
, what is the value ofcalnow.getTimeInMillis()
? – codeMagic