I am trying to create an alarm in Android when a button is clicked for the same day that the button is given but for a specific time.
The code I have is the following:
AlarmManager alarma = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar cal= Calendar.getInstance();
cal.set(Calendar.MONTH,cal.get(Calendar.MONTH));
cal.set(Calendar.YEAR,cal.get(Calendar.YEAR));
cal.set(Calendar.DAY_OF_MONTH,cal.get(Calendar.DATE));
cal.set(Calendar.HOUR_OF_DAY,20);
cal.set(Calendar.MINUTE,30);
alarma.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),pendingIntent);
However, I have a problem when setting the parameters of the alarm. I have the first two parameters set, however I can't get the third one, the pendingIntent
.
Honestly, reading the documentation on this method in the official Android documentation, it has not been very clear to me what I should place in the third parameter and the examples that I have seen on the Internet have helped me to mess up a little more than help me.
How could I set the alarm correctly?
Thank you very much.
This code is to activate an Alarm, just change the values for yours.
If you want a more explicit explanation read this .
It all depends on what you want to do when the alarm runs. It may be that you want to open an Activity, or send a broadcast intent to receive it in a Broadcast Receiver, or something else
To open an activity
To send a BroadcastIntent
The pending Intent has many uses, I leave you the PendingIntent documentation so you can investigate what else can be done
https://developer.android.com/reference/android/app/PendingIntent.html