3

I'm new to Android. I've been developing my first application which is a reminder. In the reminder I have created two EditTexts, 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");
    }

}
}
Mark
  • 33
  • 6
  • clarify what are you trying to achieve . from your question i get that you want to choose date from the datepicker and time from timepicker (for that selected date). and trigger one single alarm for the date and time combined. correct me if i am wrong. – Sagar Nayak Mar 29 '16 at 09:24
  • You are right, that's what I'm trying to achieve. The time and date selected (in my code so far) are treated separately, not combined – Mark Mar 29 '16 at 09:33

1 Answers1

1

apply this to your program-

  1. define global Calendar variable .

    Calendar selecteddate;
    
  2. 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();
    
  3. 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();
    
  4. 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.

Sagar Nayak
  • 2,138
  • 2
  • 19
  • 52
  • Thank you so much for your time and comment!! I have a question, where should I use setAlarm method that is not used? – Mark Mar 29 '16 at 11:04
  • you have already used setalarm method in your code . pass the calendar to that function. – Sagar Nayak Mar 29 '16 at 11:05