So this is my code, it works but i can't set an alarm except at 12:00 AM i want the user to set an alarm at a specific date, like the one given below but to be able to set it at any time of the day not just midnight...can anyone help ?
Calendar now = Calendar.getInstance();
pickerDate.init(
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH),
null);
pickerTime.setCurrentHour(now.get(Calendar.HOUR_OF_DAY));
pickerTime.setCurrentMinute(now.get(Calendar.MINUTE));
buttonSetAlarm = (Button)findViewById(R.id.setalarm);
buttonSetAlarm.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
Calendar current = Calendar.getInstance();
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
0);
if(cal.compareTo(current) <= 0){
Toast.makeText(getApplicationContext(), "Invalid Date/Time", Toast.LENGTH_LONG).show();
}else{
setAlarm(cal);
}
}});
}
private void setAlarm(Calendar targetCal){
String pattern = "MM/dd/yyyy";
SimpleDateFormat format = new SimpleDateFormat(pattern);
Calendar cal = Calendar.getInstance();
cal.set(pickerDate.getYear(),
pickerDate.getMonth(),
pickerDate.getDayOfMonth(),
pickerTime.getCurrentHour(),
pickerTime.getCurrentMinute(),
0);
cal.set(Calendar.MILLISECOND,0);
try {
Date date12_25 = format.parse("12/25/2017");
Lebanon.put(date12_25,"Christmas Day");
for(Date k : Lebanon.keySet()){
if(cal.getTime().equals(k)){
String val = (String)Lebanon.get(k);
info.setText("Your alarm is set for :"+k+", on: "+val);
}
}
} catch (ParseException e) {
e.printStackTrace();
}format.format(new Date());
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
Use BroadcastReceiver.
public class MyBroadcastReceiver extends BroadcastReceiver {
MediaPlayer mp;
@Override
public void onReceive(Context context, Intent intent) {
mp=MediaPlayer.create(context, R.raw.alrm );
mp.start();
Toast.makeText(context, "Alarm....", Toast.LENGTH_LONG).show();
}
}
Start Alarm-
// i = int time from calander.
int i = 12;
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()
+ (i * 1000), pendingIntent);
Toast.makeText(this, "Alarm set in " + i + " seconds",Toast.LENGTH_LONG).show();