I have a problem executing the following code:
Intent myIntent = new Intent(MedicineEntry.this, DisplaySchedule.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MedicineEntry.this, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 25);
calendar.set(Calendar.SECOND, 0);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(MedicineEntry.this, "Alarm Will Start In A Second", Toast.LENGTH_LONG).show();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
To elucidate my problem, the app should fire up according to the values set for HOUR_OF_DAY, MINUTE and SECOND - even if the device is in sleep mode. I've tried testing the code, but the app does not seem to work. I've tried replacing AlarmManager.INTERVAL_FIFTEEN_MINUTES with 5*1000 which worked fine. Please go easy with the terminologies. I've just started to program with Android this semester at college.
EDIT: This app I am creating is a 'reminder' app. The user is expected to enter the time which the app should execute.
There are basically two ways of interacting with the AlarmManager. Your post is somewhat vague about which will suit your needs.
Anyway, if you want an "Do every 15 minutes" type of alarm, use the ELAPSED mode:
long firstActivation = SystemClock.elapsedRealtime(); // and not System.currentTimeMillis()
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstActivation, INTERVAL_MILLISECS, intent);
But, if you're looking for an "Do every day at 15:30" semantics, use RTC mode, like this:
long activationTime = calendar.getTimeInMillis();
long TWENTY_FOUR_HRS = 24 * 60 * 60* 1000;
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, activationTime, TWENTY_FOUR_HRS, intent);
Two things to note at the above code:
A. I am using a waking up type of alarms. These are potentially expensive as they would wake up a sleeping device. Are you sure this is what you need (hint: most apps do not)?
B. I am using setInexactRepeating() instead of setRepeating() so to allow the alarm manager to batch together several alarms thus saving resources. If you do not have a good reason not to do so, stick to using setInexactRepeating()
Related
I have an app which needs to do task at the exact time provided just like the stock android alarm clock I have used setExactAndAllowWhileIdle() / setExact(). But BroadcastReciever fires after some minutes and sometimes after opening the app, is there any workaround for it?
Alarm Setter code
// set Alarm
Intent intent = new Intent(this.context, AlarmReceiver.class);
intent.putExtra("alarmInfo", extraInfo);
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 123456, intent, 0);
AlarmManager alarmManager = (AlarmManager) this.context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, finalTime - 400, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalTime - 400, pendingIntent);
}
BroadcastReciever
setNextAlarm();
Log.d("RESULT", "fired");
Intent alarmIntent = new Intent(context, AlarmSoundService.class);
// Log.d("extra", "alarmInfo: " + intent.getStringExtra("alarmInfo"));
alarmIntent.putExtra("alarmInfo", intent.getStringExtra("alarmInfo"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(alarmIntent);
} else {
context.startService(alarmIntent);
}
setNextAlarm() is the previous code I provided which sets the alarm for the future task.
EDIT
Ok so I have solved my problem the first is getting my alarm to fire at the exact moment so setExactAndAllowWhileIdle() / setExact() are both kind of exact so if I need to show user something and have to be accurate
I used the setAlarmClock() as the android doc explains
The system may also do some prep-work when it sees that such an alarm
coming up, to reduce the amount of background work that could
happen if this causes the device to fully wake up
The second problem was that the alarm wasn't firing up unless I open the app this was due to the fact that two alarms were firing between the interval of 5 minutes and since I was using setExactAndAllowWhileIdle()
and the android doc explains
Under normal system operation, it will not dispatch these alarms more
than about every minute (at which point every such pending alarm is
dispatched); when in low-power idle modes this duration may be
significantly longer, such as 15 minutes.
And that was it.
I want to run the function foo() when the alarm manager runs off, but I have not understood how do I do it. I saw you pass an Intent to the alarm manager, are there other ways to do that?
public void SetAlarm()
{
Calendar cal = Calendar.getInstance();
cal.set(Calendar.HOUR_OF_DAY, 19);
cal.set(Calendar.MINUTE, 03);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
AlarmManager alarmMgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmMgr.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
}
As you can see, I'm not sure how to use the alarmMgr.set function correctly. Also, do I need to run it as a service, so if the application will be exited, then it'll still work ?
Thanks!
I saw you pass an Intent to the alarm manager
You pass a PendingIntent to AlarmManager.
are there other ways to do that?
The only way you can use AlarmManager is with a PendingIntent. The purpose of AlarmManager is to give your app control at points in time in the future when your app is no longer running.
Also, do I need to run it as a service, so if the application will be exited, then it'll still work ?
No. The goal of AlarmManager specifically is to allow your process to be terminated, yet give you control again some time in the future, so you are not tying up memory all that time.
I need my Service to do tasks every night at 02:00, and if failed in task then schedule itself to start again after 30min, but if succeeded then stop itself and start again the other night at 02:00. I managed to get it start at 02:00 but after it is triggered it just triggeres itself continiously.
My code is in Service, and here is the code itself:
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
//doTasks();
MediaPlayer player = MediaPlayer.create(this,
Settings.System.DEFAULT_NOTIFICATION_URI);
player.start();
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}
doTasks() is the tasks I need to run but because they are slow I use notification sound for testing purposes. And pendingIntent is this Service itself.
Any ideas what is it that makes my AlarmManager trigger constantly?
If your pendingIntent is set to start the Service then basically you're repeatedly creating a new alarm each time in the onStart(...) method.
AlarmManager has a mechanism where it will trigger an alarm "late" if the device was asleep when it should have triggered. In your case, because you constantly re-create the alarm, AlarmManager constantly thinks it should be triggered immediately even if the time is past 02:00:00.
Create the alarm somewhere else to prevent it being constantly re-created each time the Service starts.
The problem is that I want to delay the repeating-alarm's first fire.
for example I want this pending intent work after 10 mins of the click not right away, how to do that?
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Main.this, ReportService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar c = Calendar.getInstance();
PendingIntent pendingIntent = PendingIntent.getService(Main.this, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 112500, pendingIntent);
}
the 2nd paramater in this setInexactRepeating called triggerAtMillis
From Android developer guide:
triggerAtMillis time in milliseconds that the alarm should first go off, using the appropriate clock (depending on the alarm type). This is inexact: the alarm will not fire before this time, but there may be a delay of almost an entire alarm interval before the first invocation of the alarm.
Iam changing it and it always start on the click and does no delay, any help?
Check the
developer link here. The second parameter is triggerAtMillis. It used as time in milliseconds that the alarm should first go off
So in your case, add the extra milliseconds to c.getTimeInMillis(), after which you want to first run your pending intent. :)
check into handler.postDelayed function where you can specify a delay
If using the ELAPSED_REALTIME clock, do this
manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+180000,
180000,
pendingIntent);
Do not use zero or System.currentTimeMillis() as an offset for the ELAPSED clock.
I am returning 7 URL's that I retrieve from a Text document on a remote server.
The URL's are links to images that I want to then download and put into a cache. These URL's will change every week.
I want to retrieve these URL's from the text document only once on a fixed day of week (e.g. Monday only).
How I can do that?
1: Make a service that connect server and get URL's then write URL to a file for use in you application.
2: Then Use alarm manger to create a alarm for a specif day. You can find details here.
how do I set an alarm manager to fire after specific days of week
While creating alarm you have to add your service with your alarm object so that when alarm time come. Then it run the service.(But be careful In that time there may be no internet.)
you can add your service to alarm object e.g
/* Scheduling Alarm for URL's update service to run at One */
Log.i(TAG, "Alarm started");
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 10);
Intent versionUpdateService = new Intent(getApplicationContext(), MyURLUpdaterService.class);
PendingIntent recurringUpdate = PendingIntent.getService(getApplicationContext(), 0, versionUpdateService,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, recurringUpdate);
Log.i(TAG, "Alarm ended");