I'm new to Android. I've been developing my first application which is a reminder. In the reminder I have created two EditText
s, one for choosing time from TimePickerDialog
and the other for choosing date from DatePickerDialog
. when I include both (TimePickerDialog and DatePickerDialog), times that are set for them are treated as seperate times and each one of the will go off when it's supposed to. Basically, two alarms will go off, not one.
How should I include a TimePickerDialog and a DatePickerDialog so that they are treated as one alarm when they're set?
Any help would be much appreciated
Here is my MainActivity class(I deleted my TimePickerDialog part from it):
MainActivity.java
public class MainActivity extends Activity {
EditText editText;
TextView textView;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView) findViewById(R.id.textView);
spinner = (Spinner) findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(MainActivity.this,
R.array.Alarms, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, parent.getItemAtPosition(position) + " is selected", Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
editText = (EditText) findViewById(R.id.editText);
editText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
openTimePickerDialog(false);
}
});
}
private void openTimePickerDialog(boolean is24r) {
Calendar calendar = Calendar.getInstance();
TimePickerDialog timePickerDialog =
new TimePickerDialog(
MainActivity.this,
onTimeSetListener,
calendar.get(Calendar.HOUR_OF_DAY),
calendar.get(Calendar.MINUTE),
is24r);
timePickerDialog.setTitle("Set time");
timePickerDialog.show();
}
TimePickerDialog.OnTimeSetListener onTimeSetListener =
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
textView.setText(hourOfDay + ":" + minute);
setAlarm(calendar);
setAlarmTwo(calendar);
}
};
private void setAlarm(Calendar targetCal) {
if (spinner.getSelectedItem().toString().equals("none")) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 2, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
textView.setText(
"\n\n***\n"
+ "One shot alarm " + "/n" + targetCal.getTime());
}
}
private void setAlarmTwo(Calendar targetCalendar) {
if (spinner.getSelectedItem().toString().equals("two mins")) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 5, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
targetCalendar.getTimeInMillis(),
TimeUnit.MINUTES.toMillis(2),
pendingIntent);
textView.setText(
"\n\n***\n"
+ "The alarm will go off every 2 minutes " + "\n" + targetCalendar.getTime() + "\n"
+ "***\n");
}
}
}
apply this to your program-
define global Calendar variable .
Calendar selecteddate;
for showing datepicker , define a calendar first and then the datepicker.
final Calendar c = Calendar.getInstance();
int mYear = c.get(Calendar.YEAR);
int mMonth = c.get(Calendar.MONTH);
int mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog startdatepicker = new DatePickerDialog(MainActivity.this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
final Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
selecteddate = c;
}
}, mYear, mMonth, mDay);
startdatepicker.show();
then for the timepicker.
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this,
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
selecteddate.set(selectedinitialdate.get(Calendar.YEAR), selecteddate.get(Calendar.MONTH), selecteddate.get(Calendar.DAY_OF_MONTH), hourOfDay, minute);
}
}
, 10, 10, false
);
timePickerDialog.show();
then you have a calendar variable which is ready to be assigned to the alarm function .
pass the selecteddate to the function and you will trigger one alarm at the time and date selected by the user.
private void setAlarm(Calendar targetCal) {
Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), 2, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
}
let me know if this works for you.