public class setAlarm extends AppCompatActivity {
final Calendar setat = Calendar.getInstance();
Calendar[] calendar = new Calendar[8];
public void dimensions(){
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * 0.62), (int) (height * 0.4));
}
public void clock(){
final TextView ora = (TextView)findViewById(R.id.clock);
findViewById(R.id.clock).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Calendar time = Calendar.getInstance();
int hour = time.get(Calendar.HOUR_OF_DAY);
int minute = time.get(Calendar.MINUTE);
TimePickerDialog timePickerDialog;
timePickerDialog = new TimePickerDialog(setAlarm.this, new TimePickerDialog.OnTimeSetListener(){
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute){
ora.setText(selectedHour + ":" + selectedMinute);
setat.set(Calendar.HOUR_OF_DAY, selectedHour);
setat.set(Calendar.MINUTE, selectedMinute);
}
}, hour, minute, true);
timePickerDialog.setTitle("Select Time");
timePickerDialog.show();
}
});
}
public void alarm(){
clock();
findViewById(R.id.confirm).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox[] boxes = new CheckBox[8];
boxes[0] = (CheckBox)findViewById(R.id.check0);
boxes[1] = (CheckBox)findViewById(R.id.check1);
boxes[2] = (CheckBox)findViewById(R.id.check2);
boxes[3] = (CheckBox)findViewById(R.id.check3);
boxes[4] = (CheckBox)findViewById(R.id.check4);
boxes[5] = (CheckBox)findViewById(R.id.check5);
boxes[6] = (CheckBox)findViewById(R.id.check6);
if(boxes[0].isChecked()) {setAlarm(1);}
if(boxes[1].isChecked()) {setAlarm(2);}
if(boxes[2].isChecked()) {setAlarm(3);}
if(boxes[3].isChecked()) {setAlarm(4);}
if(boxes[4].isChecked()) {setAlarm(5);}
if(boxes[5].isChecked()) {setAlarm(6);}
if(boxes[6].isChecked()) {setAlarm(7);}
Toast.makeText(setAlarm.this, "Alarm has been set!", Toast.LENGTH_SHORT).show();
//finish();
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.alarm);
dimensions();
alarm();
}
public void setAlarm(int day){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, setat.get(Calendar.HOUR_OF_DAY));
calendar.set(Calendar.MINUTE, setat.get(Calendar.MINUTE));
calendar.set(Calendar.SECOND, 0);
long time = calendar.getTimeInMillis() - Calendar.getInstance().getTimeInMillis();
Log.d("day", String.valueOf(day));
Log.d("time", String.valueOf(setat.get(Calendar.HOUR_OF_DAY)) + ":" + String.valueOf(setat.get(Calendar.MINUTE)));
Log.d("actual", String.valueOf(Calendar.getInstance().get(Calendar.MINUTE)));
Intent intent = new Intent(getApplicationContext(), NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 100, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getApplicationContext().getSystemService(ALARM_SERVICE);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
}
}
I want to set an alarm at a specified hour and minute and it should repeat that alarm every week on selected days. The problem is that the after confirming the alarm it is firing immedietly or not on the selected time. I have searched this problem before but i couldn't find anything useful for me.
If the alarm is firing immediately then the issue is probably that the time is in the past, not the future. You should try ensuring (in your alarm
method) that calendar.getTimeInMillis()
is greater than System.currentTimeMillis()
, and adjust the calendar instance approriately if it is not.