Wednesday, September 26, 2018

android - I am trying to set alarm on specific time using alarm manager but alarm initiated instantly?

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.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...