N
N
Nikolarsen2018-02-14 16:17:08
Java
Nikolarsen, 2018-02-14 16:17:08

How to use TimePickerDialog and DarePickerDialog?

It is necessary to show the notification at the date and time selected by the user in the dialog.
There is a code, but it does not work as it should. I select tomorrow's date and time in the dialog one minute ahead of the current one (for example, 12:20) and the notification arrives in a minute, although it should come tomorrow ... I select today's day in the dialog and the notification immediately appears, I don’t even have time to set the time ..
Somehow the date and time are processed separately, but it is necessary that this whole be ...

// запуск диалогов 
  
  public void oneTime(View view){
    
  openTimePickerDialog(true);
  openDatePickerDialog(null);
    
  
  }

  private void openTimePickerDialog(boolean is24r){
    Calendar calendar = Calendar.getInstance();

    timePickerDialog = new TimePickerDialog(
      NoteActivity.this, 
      onTimeSetListener, 

      calendar.get(Calendar.HOUR_OF_DAY), 
      calendar.get(Calendar.MINUTE), 
      is24r);
    timePickerDialog.setTitle("Set Alarm Time");  

    timePickerDialog.show();

  }

    OnTimeSetListener onTimeSetListener
    = new OnTimeSetListener() 
  {

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

      Calendar calNow = Calendar.getInstance();
      Calendar calSet = (Calendar) calNow.clone();


      calSet.set(Calendar.HOUR_OF_DAY, hourOfDay);
      calSet.set(Calendar.MINUTE, minute);
      calSet.set(Calendar.SECOND, 0);
      calSet.set(Calendar.MILLISECOND, 0);



      if(calSet.compareTo(calNow) <= 0){
        //Today Set time passed, count to tomorrow
        //calSet.add(Calendar.DATE, 1);

      }
      
       time = new SimpleDateFormat("HH:mm").format(calSet.getTime());		
      
    setAlarm(calSet);
    
    }};

  //////////////////////////date
  

  public void openDatePickerDialog(View v) {
        new DatePickerDialog(NoteActivity.this, d,

               dateAndTime.get(Calendar.YEAR),
               dateAndTime.get(Calendar.MONTH),				 
               dateAndTime.get(Calendar.DAY_OF_MONTH))

      .show();
    }

    DatePickerDialog.OnDateSetListener d =new DatePickerDialog.OnDateSetListener() {
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

      Calendar calNow = Calendar.getInstance();
      Calendar dateAndTime = (Calendar) calNow.clone();

      dateAndTime.set(Calendar.YEAR, year);
            dateAndTime.set(Calendar.MONTH, monthOfYear);
            dateAndTime.set(Calendar.DAY_OF_MONTH, dayOfMonth);

      if(dateAndTime.compareTo(calNow) <= 0){
        //Today Set time passed, count to tomorrow
        //dateAndTime.add(Calendar.DATE, 0);

      }

      date = new SimpleDateFormat("EEE, d MMM ").format(dateAndTime.getTime());
      
      
      
      
        setAlarm(dateAndTime);
    
      
      
    }};
  
    
    
  private void setAlarm(Calendar targetCal){

editTextNote.setText(date+time+" + ");

    final int id = (int) System.currentTimeMillis();
    
    Intent intent = new Intent(getBaseContext(), AlarmBroadcastReceiver.class);	
    PendingIntent pendingIntent = PendingIntent.getBroadcast(getBaseContext(), id, intent, 0);
    AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);
  }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question