How to edit and delete alarm time in Android? - android

I have built an alarm which works properly:
public static void setAlarm(int hours, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}
Now, how can I edit the alarm time and delete the alarm?

For cancelling the alarm you may call AlarmManager.cancel with your PendingIntent.
Intent myIntent = new Intent(PreferenceHelper.getContext(), TimeManagementAdapter.AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0);
alarmManager.cancel(pendingIntent);
For editing the alarm, you have the option to first cancel it and re-add it or you may add PendingIntent.FLAG_UPDATE_CURRENT as the last option when scheduling a new alarm.
PendingIntent pendingIntent = PendingIntent.getBroadcast(PreferenceHelper.getContext(), 0, myIntent, 0, PendingIntent.FLAG_UPDATE_CURRENT);
Possible duplicate Cancel an AlarmManager pendingIntent in another pendingintent

Related

Why isn't my alarm going off? (android)

My alarm does not go off at the specific time or at all (I have tried setting it to one minute of current time). I have made sure that setAlarm() is going off because the log Log.d("asas", "adasd"); goes off. I do not know what is wrong, if you see how I can fix this please let me know. Thanks
public void setAlarm(){
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, DrawOverAppsService.class);
PendingIntent alarmIntent = PendingIntent.getActivity(MainActivity.this, 123, intent, 0);
if(sp.getBoolean("startChecked",false) == true) {
Log.d("asas", "adasd");
java.util.Calendar calendar = java.util.Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(java.util.Calendar.HOUR_OF_DAY, 19);
alarmMgr.setRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
if(sp.getBoolean("startChecked",false) == false) {
Log.d("asdasd","asdasdasd");
alarmMgr.cancel(alarmIntent);
}
}
Changed PendingIntent alarmIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0); to PendingIntent alarmIntent = PendingIntent.getService(MainActivity.this, 0, intent, 0);

Android cancelling pending intent not working

I am facing this really weird problem. I have made a working alarm system. But when I delete the alarm, it calls it.
AlarmBuddy.java (Working class to set original alarm)
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.MONTH, m-1);
calendar.set(Calendar.DAY_OF_MONTH, d);
calendar.set(Calendar.HOUR, 8);
calendar.set(Calendar.MINUTE, repeat%60);
final Intent myIntent = new Intent(this.context, AlarmMiddle.class);
myIntent.putExtra("name", name);
myIntent.putExtra("id", repeat);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
super.onBackPressed();
PendingIntent pending_intent = PendingIntent.getBroadcast(AlarmBuddy.this, repeat, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pending_intent);
ListRecord.java THE FLAW
public Boolean cancelAlarm(int id){
Intent myIntent = new Intent();
PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_CANCEL_CURRENT).cancel();
return false;
}
This function is to cancel the alarm. This has some problem to say this again:
The code can SET AND CALL an alarm. But when I delete it from a DIFFERENT class, it immediately calls the alarm. Maybe I am just a noob. Or maybe this is an unsolvable problem.
This is working for me at all in my application see link
Intent intent = new Intent(getBaseContext(), Your_Service.class);
PendingIntent pendingIntent = PendingIntent.getService(getBaseContext(), reqcode, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
To cancel the alarm, you need to use the same PendingIntent as you used to set it. You should also tell the AlarmManager to cancel the alarm as well as canceling the PendingIntent. Try this:
public Boolean cancelAlarm(int id) {
Intent myIntent = new Intent(context, AlarmMiddle.class);
PendingIntent pending_intent = PendingIntent.getBroadcast(context, id, myIntent,
PendingIntent.FLAG_NO_CREATE);
if (pending_intent != null) {
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pending_intent);
pending_intent.cancel();
}
return false;
}

How to start AlarmService every day

I have an AlarmService that is initialized when the devicve boots, starts at 6 am, ends at 6 pm and repeats hourly.
How do I make sure that it starts again after I cancel it at 6 pm?
This is my BootReceiver:
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
// assign RefreshService class
Intent alarmIntent = new Intent(context, RefreshService.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(context, alarmManagerRequestCode, alarmIntent, 0);
// set the alarm
// it starts at 6 am and repeats once an hour
// elapsed_realtime is used to save ressources
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, firstRefresh,
AlarmManager.INTERVAL_HOUR,
alarmPendingIntent);
}
So it is initialized when the device boots and starts at 6 am (firstRefresh) and repeats hourly.
To have it stop I would put the following into my RefreshService(lastRefresh is 6 pm in milliseconds):
if (System.currentTimeMillis() >= lastRefresh) {
Intent alarmIntent = new Intent(this, RefreshService.class);
PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(this, BootReceiver.alarmManagerRequestCode, alarmIntent, 0);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(alarmPendingIntent);
}
Appreciate y'alls help!
Thanks #Opiatefuchs for pointing that out:
The only thing you have to do is setting the alarm after cancelling it and setting the first time it's fired to needed time the next day.
Use calender, AlarmManager and pendingIntent for this-
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 5); // For 5 AM or 6 AM
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, NewClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);
Use setRepeating:
here is the nice example:
https://stackoverflow.com/a/13879448/4617458

PendingIntent For Multiple Alarms

StartAlarmMethod();
AlarmManager am=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context,AlarmManagerBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, day);
calendar.set(Calendar.HOUR_OF_DAY, hours);
calendar.set(Calendar.MINUTE, minutes);
calendar.set(Calendar.SECOND, seconds);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 , pi);
I am using PendingIntents for multiple Alarms. How can i properly use TAGS, so i can later cancel only the Alarm that i don't want to use anymore?
Right now i'm seting Alarms with the code above. But if i set more than 1, using the code appears bellow i stop all upcoming Alarms. Instead of that i want to be able to identify somehow this PendingIntents and cancel only the ones that are not required.
CancelMethod();
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(sender);
To support multiple alarms send a different request code each time you call it in the getBroadcast method. To cancel just send the same request code.
Below code you can loop it within for.
Note: replace i with any value and when cancelling pass the same value to cancel the particular alarm.
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, i, alarmIntent, 0);
manager.set(AlarmManager.RTC_WAKEUP, 10000, pendingIntent);
To cancel, send the requestcode which you want to cancel,
Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, i, intent, 0);
alarmManager.cancel(sender);

Repeating Alarmmanager to stop at quite/night hours

I have an alarmmanager set to fire every 3 hours a day and its working fine. Now my problem is i dont want alarmmanager to fire at night hours i.e from 9pm to 9am on next day.
My code:
Intent myIntent = new Intent(context, MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
//PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MILLISECOND, 0);
long interval = 3*60*60*1000;
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), interval, pendingIntent);
and to stop this alarmmanager repeating, i used this snippet to stop manually.
Intent myIntent = new Intent(context, MyScheduledReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Now how do i check if the time is in night hours or at day hours?

Categories

Resources