1

I am new to android studio and would like to create a notification scheduler for a specific time given by users.

However, the date and time dialog pick from EditText and I have set Button to get the time and push to a notification. I am stuck at what code at 'set' button I should write.

            case R.id.setBtn:

// stuck at here....//


                String text = dateTimeIn.getText().toString().trim();

                if (text.isEmpty()) {
                    Toast.makeText(this, "Please select date and time", Toast.LENGTH_SHORT).show();
                } else {
                    dateTimeIn.getText().toString().trim();
                    Toast.makeText(this, "Done!", Toast.LENGTH_SHORT).show();
                }


                break;

            case R.id.cancelBtn:
                alarmManager.cancel(alarmIntent);
                Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
                break;

this my full code...


    private int notificationId = 1;

    EditText dateTimeIn;

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

        findViewById(R.id.setBtn).setOnClickListener(this);
        findViewById(R.id.cancelBtn).setOnClickListener(this);

        dateTimeIn = findViewById(R.id.dateTimeInput);

        dateTimeIn.setInputType(InputType.TYPE_NULL);


        dateTimeIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showDateTimeDialog(dateTimeIn);
            }
        });

    }

    private void showDateTimeDialog(final EditText dateTimeIn) {

        final Calendar calendar=Calendar.getInstance();
        DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {

                calendar.set(Calendar.YEAR, year);
                calendar.set(Calendar.MONTH, month);
                calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                TimePickerDialog.OnTimeSetListener timeSetListener=new TimePickerDialog.OnTimeSetListener() {
                    @Override
                    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {

                        calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
                        calendar.set(Calendar.MINUTE, minute);

                        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("dd-MM-yy HH:mm");
                        dateTimeIn.setText(simpleDateFormat.format(calendar.getTime()));

                    }
                };

                new TimePickerDialog(JadwalWc.this,timeSetListener,calendar.get(Calendar.HOUR_OF_DAY),calendar.get(Calendar.MINUTE),false).show();

            }
        };
        new DatePickerDialog(JadwalWc.this,dateSetListener,calendar.get(Calendar.YEAR),calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH)).show();
    }

    @Override
    public void onClick(View view) {

        EditText editText = findViewById(R.id.editText);
        EditText dateTimeIn = findViewById(R.id.dateTimeInput);


        Intent intent = new Intent(JadwalWc.this, AlarmReceiver.class);
        intent.putExtra("notificationId", notificationId);
        intent.putExtra("message", editText.getText().toString());



        PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);



        switch (view.getId()) {
            case R.id.setBtn:


                String text = dateTimeIn.getText().toString().trim();

                if (text.isEmpty()) {
                    Toast.makeText(this, "Please select date and time", Toast.LENGTH_SHORT).show();
                } else {
                    dateTimeIn.getText().toString().trim();
                    Toast.makeText(this, "Done!", Toast.LENGTH_SHORT).show();
                }


                break;

            case R.id.cancelBtn:
                alarmManager.cancel(alarmIntent);
                Toast.makeText(this, "Canceled", Toast.LENGTH_SHORT).show();
                break;




        }


    }
} 

and this my receiver code...

    private static final String CHANNEL_ID = "SAMPLE_CHANNEL";

    @Override
    public void onReceive(Context context, Intent intent) {
        int notificationId = intent.getIntExtra("notificationId", 0);
        String message = intent.getStringExtra("message");

        Intent mainIntent = new Intent(context, TipsWc.class);
        PendingIntent contentIntent = PendingIntent.getActivity(
                context, 0, mainIntent, 0
        );

        NotificationManager notificationManager =
                (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence channel_name = "My Notification";
            int importance = NotificationManager.IMPORTANCE_HIGH;

            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channel_name, importance);
            notificationManager.createNotificationChannel(channel);
        }


        NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID);
        Uri soundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        Bitmap picture = BitmapFactory.decodeResource(context.getResources(),R.drawable.notif2);

        long vibrate[]={100,500,100,500};
        builder.setSmallIcon(R.drawable.nssmall);
        builder.setContentTitle("Your aquarium maintenance schedule");
        builder.setLargeIcon(picture);
        builder.setStyle(new NotificationCompat.BigPictureStyle()
            .bigPicture(picture)
            .bigLargeIcon(null));
        builder.setContentText(message);
        builder.setContentIntent(contentIntent);
        builder.setPriority(NotificationCompat.PRIORITY_HIGH);
        builder.setAutoCancel(true);
        builder.setLights(Color.YELLOW, 200, 200);
        builder.setSound(soundUri);
        builder.setVibrate(vibrate);



        notificationManager.notify(notificationId, builder.build());

    }
}

Zoe
  • 27,060
  • 21
  • 118
  • 148
Azer89
  • 33
  • 6
  • Check out [this](https://stackoverflow.com/questions/23106006/how-schedule-local-notifications-android). You need to use `alarmManager.setExact` in your `onTimeSet` callback – aytek Dec 23 '20 at 22:42
  • @aytek thanks for reply, i put the code `switch (view.getId()) { case R.id.setBtn: Calendar cal = Calendar.getInstance(); alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), alarmIntent); ` indeed the notification appear, but not schedulled, – Azer89 Dec 24 '20 at 00:23

0 Answers0