How to create a notification alarm with datepicker and timepicker?

1

I have a datepicker and timepicker, their values are shown in their respective EditText, as an achievement that when I press a button I take the values of the date and time, and I create a notification.

Button botonhora, botonfecha;
EditText edithora, editfecha;
private  int dia, mes, año, hora, minutos;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_recuerdame);

    botonhora =(Button) findViewById(R.id.buttonhora);
    edithora =(EditText) findViewById(R.id.edithora);

    botonfecha =(Button) findViewById(R.id.buttonfecha);
    editfecha =(EditText) findViewById(R.id.editfecha);

    botonhora.setOnClickListener(this);
    botonfecha.setOnClickListener(this);
}

@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
    if (v==botonfecha){
        final Calendar calendario = Calendar.getInstance();
        dia=calendario.get(Calendar.DAY_OF_MONTH);
        mes=calendario.get(Calendar.MONTH);
        año=calendario.get(Calendar.YEAR);

        DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                editfecha.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
            }
        }
        ,dia, mes, año);
        datePickerDialog.show();
    }

    if (v==botonhora) {
        final Calendar calendario = Calendar.getInstance();
        minutos = calendario.get(Calendar.HOUR_OF_DAY);
        hora = calendario.get(Calendar.MINUTE);

        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {
            @Override
            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                edithora.setText(hourOfDay+":"+minute);
            }
        } ,hora, minutos, false);
        timePickerDialog.show();
    }
}
    
asked by Nelson Emmanuel 09.05.2017 в 14:14
source

1 answer

0

The values can be obtained within the methods onDateSet() and onTimeSet() respectively, you can call for example a method that generates the notification with this data:

 DatePickerDialog datePickerDialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                editfecha.setText(dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
                //Crea notificación.
                showNotification("la fecha es: " + dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
            }
        }

For this you must create a method to generate the notification, example:

public void showNotification(String message) {
    NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(this)
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("My notification")
                    .setContentText(message);
    NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mNotificationManager.notify(001, mBuilder.build());
}

This way you can generate your notification.

    
answered by 09.05.2017 в 17:18