How to test Alarm Manager which set for a INTERVAL_DAY - android

I written a small application which invokes a service using AlarmManager with interval of the day. To test this app, i am using emulator 5.1 . When i set manually date and time(for current date or future date time) the AlarmManager invokes service for that day. When i am trying to change the date for checking whether it will work for other day by changing date then its not invoking service.
Here is my code.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(calendar.HOUR_OF_DAY,13);
calendar.set(calendar.MINUTE,20);
calendar.set(Calendar.SECOND,10);
alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,BackgroundService.class);
pendingIntent = PendingIntent.getService(this, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP,calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,pendingIntent);
Toast.makeText(this, "Alarm Set", Toast.LENGTH_SHORT).show();
BackgroundService is service which popup with a Toast .
I have 2 issues.
How can test AlarmManager , which we set with Interval of a day?
The Alarm Manager is not exactly invoking at the second. For example , i set the alarm at 13:20:10 but its coming at 13:20:some Random second
Your help is much appreciated.
Thank you.

RTC (Real Time Clock) is the clock that you can adjust, so you could possibly just change the current time programatically for a test case. See this answer
You're using a alarmManager.setInexactRepeating method, which doesn't guarantee an exact time of alarm delivery. For more precise timings, use alarmManager.setRepeating, however keep in mind that it's not really recommended, as most use cases do not require exact timings and it will cause higher battery usage. See inexactRepeating and repeating

Related

AlarmManager use setExactAndAllowWhileIdle so it repeats

I have an app that uses an AlarmManager to schedule a service to repeat at a set frequency, say every hour.
From Android 6, devices will drop into Doze mode, this mode ignores wakelock and Alarms, potentially running them in the maintenance window. I would prefer that the Alarm executes when it is scheduled even if the device is in low-power idle modes.
I understand i can use setExactAndAllowWhileIdle to execute the alarm in Doze but this will excute only once. I cannot see any method that has this functionality but will repeat at a set frequency.
for example i use the code below to fire an alarm every hour. Is there a way to use setExactAndAllowWhileIdle so it repeats?
// get a Calendar object with current time
Calendar cal2 = Calendar.getInstance();
// add 5 minutes to the calendar object
cal2.add(Calendar.MINUTE, 1);
Intent intentTracking = new Intent(getApplicationContext(), TrackingAlarmReceiver.class);
// In reality, you would want to have a static variable for the request code instead of 192837
PendingIntent sender3 = PendingIntent.getBroadcast(getApplicationContext(), 192839, intentTracking, PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
AlarmManager am3 = (AlarmManager) getSystemService(ALARM_SERVICE);
//am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
am3.setRepeating(AlarmManager.RTC_WAKEUP, cal2.getTimeInMillis(), ((Integer.parseInt(carerTrackingInteval)) * 60000 ), sender3);
ADM is spot-on.
As part of the work in TrackingAlarmReceiver, you call setExactAndAllowWhileIdle() to schedule the next bit of work.
Bear in mind that the minimum granularity of such events is ~10 minutes IIRC, and that you may not have network access even though you get control.

How to use CPU to perform any operation in Deep Sleep mode

I'm new in android. I struggle with my application approximately 3 weeks. I need sent and receive packets in normal mode and sleep mode. My app must exchange data a 5 seconds. I tried using alarmmanager but on android 5 it's not works. On android 5 an interval changes it on 60 seconds. Such a solution makes the battery wears out quickly. When I use normal asynctask, not IntentService, then it works only when screen is ON and app is visible. When app is hidden or I click power OFF then exchange data stops working. What is the best solutions?
Even RTC_WAKEUP doesn't help most of the times.
Solution that worked for my app when device in deep sleep mode:
Use WakefulBroadcastReceiver combined with AlarmManager.
Service is started by startWakefulService() and when it is finished, it releases the wake lock by calling completeWakefulIntent(intent). So the device will be allowed to go back to sleep.
I'm not adding any code. Search for examples on how to use WakefulBroadcastReceiver with AlarmManager. Even WakefulBroadcastReceiver doc has some template code.
Also reduce the frequency of alarm so you can avoid draining so much battery.
You can use the AlarmManager class to wake up the device at a particular time, then fire off an operation at whatever interval you'd like. Code from the docs found here:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
...
alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at 8:30 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 8);
calendar.set(Calendar.MINUTE, 30);
// setRepeating() lets you specify a precise custom interval--in this case,
// 20 minutes.
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 60 * 20, alarmIntent);
Notice the last line of this block. You can use the method setRepeating() to set whatever interval you'd like.

Calendar instance in android for AlarmManager

I am developing an app which displays notifications by using AlarmManager.
For that I'm taking the user input values for hour, minute and second.
Something like:
int hour = 4;
int min = 40;
int sec =36
Calendar Calendar_Object = Calendar.getInstance();
Calendar_Object.set(Calendar.HOUR, hour);
Calendar_Object.set(Calendar.MINUTE, min);
Calendar_Object.set(Calendar.SECOND, sec);
Intent myIntent = new Intent(MyView.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MyView.this,0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, Calendar_Object.getTimeInMillis(), myIntent);
Notifications and the rest of the code work fine, but the problem is that instead of 4:40:36 the notifications get invoked on the current time(as soon as i run/debug the app).
I think there is some problem in Calender_Object part.
Looking for a solution.
Thanks in advance.
Please note: Alarms will be executed immediately, if the notification time has elapsed already.
As a workaround you might want to consider a date part too. Or just a variable in memory which acts as a boolean if the time has elapsed or not.
From the Docs:
If the stated trigger time is in the past, the alarm will be triggered immediately. If there is already an alarm for this Intent scheduled (with the equality of two intents being defined by filterEquals(Intent)), then it will be removed and replaced by this one.
Also please consider the API level 19 version of how AlarmManager works.

How to set time of choice in a Calendar object?

Sorry if the question is a bit misleading.
I asked another question earlier and by its help I got this far. Here is the problem.
I'm supposed to launch a Service at a particular time. Now... the service starts if I launch it instantly by using current time from a Calendar object, but when I set the time manually, the service doesn't launch. Here's the code:
Calendar myCal = Calendar.getInstance();
myCal.set(Calendar.HOUR_OF_DAY, 21);
myCal.set(Calendar.MINUTE, 4);
// Will use putExtras here to give service the details of which profile to activate
Intent intent = new Intent(Create.this, AutoBot.class);
PendingIntent pintent = PendingIntent.getService(Create.this, 0, intent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, myCal.getTimeInMillis(), 5*1000, pintent);
Now this should technically work, but its not. And that is why I need your help :(
Also, If I have multiple triggers (alarms per se), what would the third argument of setRepeating be?
I think you need to set up day month and year as well. Date in millis is from 1 Jan 1970
I think this link may answer your question. It explains a there is a limitation in
the system where the wake lock the alarm manager holds will be released too early when
delivering to a service. See this discussion: http://groups.google.com/group/android-developers/browse_thread/thread/2b98206d18a0192f/
Hope this helps.

Android: Alarm to be play every 30 minutes and it start from 12:30

Here i am going to use the alarm service to play the alarm at every 30 minutes. Right now i have set it to play it at every 10 second from the Every start.
Here is the Code:
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_layout);
Intent myIntent = new Intent(SettingsActivity.this, MyAlarmService.class);
pendingIntent = PendingIntent.getService(SettingsActivity.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 10*1000, pendingIntent);
}
Now the Problem is, I want to start the alarm from the 12:30 not from the time application start and it should repeatedly play at evert 30 minutes. like 1:00, 1:30, 2:00 . . . etc
So what changes i have to do in my code ?
To start a service at specific time.
see this post maybe helpful:
Using Alarmmanager to start a service at specific time
Set your initial alarm time for 12:30 using the Set method.
When the alarm fires, then set up your next alarm time and keep doing that until you don't want the alarm any more.
You don't need a service to do such a simple task. AlarmManager is more than capable of handling this.
Try it
And Use first time -
Try another
A naive approach would be:
Start a context that will always run, such as a Thread in a Service.
Calculate the timestamp of the time you want the next alert to ring using Date and put it in "long alertTimestamp".
In a loop, calculate the timestamp right now using Date and put it in "long nowTimestamp" .
If nowTimestamp < alertTimestamp, put the thread to sleep for (alertTimestamp - nowTimestamp).
Else sound the alert and recalculate alertTimestamp, repeat.
Make sure you catch notify interruptions to the tread gracefully, that is the key to aborting the timer.
Hope this was helpful.
For repeating every 30 minutes you will need to add this to your code:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000*60*30, pendingIntent); //millisecs*seconds*minutes
But I haven't figure out how to start at specific time yet.

Categories

Resources