Failed to set Alarm for a week - android

I used the following code for setting the alarm for week(Repeating). The following code trigger my service in between 15 to 30 minute. Actually i have configured it for a week
AlarmManager alarm = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent newIntent = new Intent(context, SyncService.class);
newIntent.putExtra(Constant.TASK_ID,Constant.PROMPT_ID);
PendingIntent pIntent = PendingIntent.getService(context, 0, newIntent, 0);
//
long interval = 7 * AlarmManager.INTERVAL_DAY;
alarm.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), interval, pIntent);

Related

Alarm manager for notification is not working properly

I have used alarm manager in Android for showing some notifications, but it is not working properly.
My code in activity is:
Intent i = new Intent(this, AlarmBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
AlarmManager alarmManager = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (int) (10*60 * 1000),
pendingIntent);
And the code for receiving a notification after 15 minutes is:
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmBroadcastReceiver.class);
PendingIntent pi = PendingIntent.getService(context, 0, intent, 0);
am.cancel(pi);
am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() +
(int) ( 5*60 * 1000), pi);
use this type of AlarmManager when you use service than you need to change getService insted of getBroadCast. sometime receiver not call and also creat issue in some devices when app in background
Intent intent = new Intent(myContext, SetAlarmService.class);
intent.putExtra("salah",i);
intent.putExtra("repeat",HowManyTimeRepeatAlarm);
PendingIntent pendingIntent = PendingIntent.getService(
myContext, i, intent, 0);
AlarmManager alarmManager = (AlarmManager) myContext.getSystemService(ALARM_SERVICE);
// alarmManager.cancel(pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, reset_cal.getTimeInMillis(),60000 * 2 ,
pendingIntent);

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

Android repeating alarm calls service, sometimes alarm stops repeating

this is how i am starting a repeating service.
Intent intent2 = new Intent(Main_page.this, SyncService.class);
final AlarmManager alarm2 = (AlarmManager)
Main_page.this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pintent2 = PendingIntent.getService(Main_page.this, 0, intent2, 0);
alarm2.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 2 * 60000, pintent2);
is there any chance of alarm stops repeating.
You can cancel an intent like:
Intent intent = new Intent(context, AlarmService.class);
PendingIntent pIntent = PendingIntent.getService(context, model.id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pIntent);

Repeating alarm runs more than once

Intent myAlarm = new Intent(context, AlarmReceiver.class);
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context.getApplicationContext(), 0, myAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Calendar updateTime = Calendar.getInstance();
alarms.setRepeating(AlarmManager.RTC_WAKEUP, updateTime.getTimeInMillis(), AlarmManager.INTERVAL_DAY / 3, recurringAlarm);
Say this piece of code is executed more than once. This results in that many alarms being created (I find this info from dumpsys where it says X# wakes - X# alarms).
I've been told that setting the same request code and same intent cannot start the alarm the second time.

Cancelling alarm from other class that the one which initiated it

I set alarm from one class using this code
Intent myIntent = new Intent(ClassOne.this, AlarmService.class);
pendingIntent = PendingIntent.getService(ClassOne.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(currentDate.getTime());
long when = calendar.getTimeInMillis();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, when, (7 * 24 * 60 * 60) * 1000, pendingIntent);
Now I need to cancel the pending alarms from another class. Can I just use this code?
Intent myIntent = new Intent(ClassTwo.this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(ClassTwo.this, 0, myIntent, 0);
AlarmManager alarmManagerCancel = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManagerCancel.cancel(pendingIntent);
Or there is a better/proper way to cancel pending alarms?
As long as the PendingIntent are equivalent as the one you used to register the alarm, it doesn't matter where you call cancel() from. Two PendingIntents are equivalent (equals() returns true) if the underlying intents and request codes are the equivalent.

Categories

Resources