Search code examples
androidalarmmanagertimepicker

Trigger an alarm in specified days and times


I have alarm and notification and they are working fine but i need to trigger alarm in specific time which inputted by user and days which is constant:

1.tonight at the timepicker time 2.tomorrow at the timepicker time 3.3 days later at the timepicker time 4.14 days later at the timepicker time 5.30 days later at the timepicker time 6.90 days later at the timepicker time

here is my code :

AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
            TimePicker timePicker1;
            timePicker1 = (TimePicker) findViewById(R.id.timePicker1);
            // ---get current date and time---
            Calendar calendar = Calendar.getInstance();


            // ---PendingIntent to launch activity when the alarm
            // triggers---
            Intent intent = new Intent(List2.this,
                    DisplayNotifications.class);
            intent.putExtra("message", esmdars);
            intent.putExtra("Title", "alarm");
            PendingIntent pendingIntent = PendingIntent.getBroadcast(
                    List2.this, 0, intent, 0);

            // ---sets the alarm to trigger---

            alarmManager.set(
                    AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis()
                            + (TimeUnit.SECONDS.toMillis(30)),
                    pendingIntent);

alarm triggers in this section and shows the notification for example here is activated after 30 seconds:

alarmManager.set(
                    AlarmManager.RTC_WAKEUP,
                    System.currentTimeMillis()
                            + (TimeUnit.SECONDS.toMillis(30)),
                    pendingIntent);

how can i activated in days and in a specific time of that day ?


Solution

  • Try this:

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, hour);
    calendar.set(Calendar.MINUTE, minutes);
    calendar.set(Calendar.DAY_OF_MONTH, day_of_month);
    calendar.set(Calendar.MONTH, month);
    calendar.set(Calendar.YEAR, year);
    alarmMgr.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);