I have an alarm that works fine if i am interacting(using) with my application but it dose not works if I set it for next day and not interacting with my app.Therefore I am getting doubt is this because my application process is not running at that time.
here is what I am doing
Calendar calSet = Calendar.getInstance();
calSet.set(Calendar.HOUR_OF_DAY, selectedhour);
calSet.set(Calendar.MINUTE, selectedminute);
calSet.set(Calendar.YEAR, year);
calSet.set(Calendar.MONTH, monthOfYear);
calSet.set(Calendar.DATE, dayOfMonth);
alarm = new Intent(ActivityA.this, Service.class);
pendingIntent = PendingIntent.getService(getApplicationContext(), i++,alarm, 1);
alarmanager.set(AlarmManager.RTC_WAKEUP, calSet.getTimeInMillis(),pendingIntent);
From AlarmManager
AlarmManager provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.
In simple way, it will work until your device has been rebooted.
You can read Android AlarmManager after reboot where #CommonsWare has been given a link of his sample application which persists Alarm even after device reboot.
Please ignore below section, it seems not valid. I will remove in future
You can read more about application kill at How to create a persistent AlarmManager, and How to save Alarm after app killing? can give you the idea about how to handle such issue (to persist alarm if application has been killed).
Yes it worked but proper understanding see doc.
Note: The Alarm Manager is intended for cases where you want to have your application code run at a specific time, even if your application is not currently running. For normal timing operations (ticks, timeouts, etc) it is easier and much more efficient to use Handler.
see here http://developer.android.com/reference/android/app/AlarmManager.html
Looking at the AlarmManager documentation..
http://developer.android.com/reference/android/app/AlarmManager.html
I don't see anywhere where it states that killing your app will remove all alarms that have been scheduled by that app. More specifically it states if your app is not started, it will start it for you.
I have done my own testing and can validate this by..
Setting an alarm 5 sec in the future.
Then closing app from recents.
Then watching logs for my broadcast to be received.
Keeping in mind this was done with a signed apk.
Calendar cal = Calendar.getInstance();
cal.add(Calendar.MILLISECOND, 5000);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);
I would also keep in mind what Pankaj Kumar said about the restarting alarms on boot. That is the one place you need to cover yourself, because AlarmManager does clear all alarms on device restart.
We need to enable our app in autostart manager in app manager, some handsets like Vivo v5,
In Vivo v5, we can find out this menu in
iManager > App Manager > Auto Start Manager > Enable our app here.
Then your alarm / alarm manager will trigger alarm if the app is killed or closed.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
In my app I have tried to set an alarm using this code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.YEAR, 2016);
calendar.set(Calendar.MONTH, 1);
calendar.set(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.HOUR_OF_DAY, 15);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 1);
calendar.set(Calendar.MILLISECOND, 1);
Intent intent = new Intent(G.context, AlarmService.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext, 1010, intent, PendingIntent.FLAG_UPDATE_CURRENT);
G.alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
It works when I set the alarm for 1 minute or later, but not when I set the alarm for 30 or 40 minutes later. While I am not on the phone for 30 minutes, after 30 minutes the alarm did not work, and when the phone screen is on the alarm worked...
Any help would be appreciated.
Ok that's because after a long time (let's say more than 5 min) your app is Paused and then the phone is "asleep" with lock screen. When you unlock it then you get your Alarm because it was queued.
To solve this problem, you need to implement a Wake Lock and add it's permission to your manifest file.
According to Google Documentation "A wake lock is a mechanism to indicate that your application needs to have the device stay on."
This is the line you have to add in your manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
You'll need to implement this object in your onReceive() method in your BroadcastReceiver. You can follow this tutorial that makes use of all you need.
https://www.javacodegeeks.com/2012/09/android-alarmmanager-tutorial.html
Also, note that you'll keep the device "awake" therefore you'll keep the phone processing affecting the battery life, but still you get to be able to get the alarm even if the screen is locked.
Hope it helps!
Since API 19 set() is treated as inexact and may be delayed. If you really need a precise alarm, you should use setExact() (available since API 19) instead :
Note: Beginning in API 19, the trigger time passed to this method is
treated as inexact: the alarm will not be delivered before this time,
but may be deferred and delivered some time later. The OS will use
this policy in order to "batch" alarms together across the entire
system, minimizing the number of times the device needs to "wake up"
and minimizing battery use. In general, alarms scheduled in the near
future will not be deferred as long as alarms scheduled far in the
future.
This change happen on 19+ device (obviously) but also only if the APK's target API is 19+, so you can
change the target API to 18
or use Build.VERSION.SDK_INT to know which method to use.
Additionnaly when you use a *_WAKE_UP alarm, the alarm manager garantees that the device will be awake long enough to execute the receiver's method, but not the service it may launch :
The Alarm Manager holds a CPU wake lock as long as the alarm
receiver's onReceive() method is executing. This guarantees that the
phone will not sleep until you have finished handling the broadcast.
Once onReceive() returns, the Alarm Manager releases this wake lock.
This means that the phone will in some cases sleep as soon as your
onReceive() method completes. If your alarm receiver called
Context.startService(), it is possible that the phone will sleep
before the requested service is launched.
The support v4 library provides a usefull helper class to handle this case : WakefulBroadcastReceiver
In you case, as you are using a service pending intent, I am not sure what wake garanties apply.
i use service
public class AlarmService extends Service {
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startid) {
//This is my Code and set Alarm
stopSelf();
}
}
I'm scheduling repeating alarms in order to execute service one a period of time.
// Set the alarm to start at approximately 24:05 a.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 24);
calendar.set(Calendar.MINUTE, 5);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, );
Some question about the implementation :
What happens if user delete the application (including the service), will something crash on the next alarm ?
Again if user delete the application, the alarms will continue running ? If so, how can I cancel any repeating alarms ?
Where/When is the best place/time to initiate the repeating alarms ? Should I wrap it with "only once" block ?
I'm not completely sure but I can give answers for your questions;
1- If user deletes your application and service, I think anything crash on the next alarm because I think your application is running on the service. (if there is no service, alarm does not works i think)
2- If user deletes your app, alarm will works but if service doesn't works, alarm does not works.
3- I think there is some options for repeating alarms and user can select one of this options.
I'm building an app and I need to schedule a notification that remember the user to access the app. I need this notification to be shown a month ahead of the last time the app was used
AlarmManager has access to the system alarm services. With the help of AlarmManager you can schedule execution of code in future. AlarmManager object can’t instantiate directly however it can be retrieved by calling Context.getSystemService(Context.ALARM_SERVICE). AlarmManager is always registered with Intent. When an alarm goes off, the Intent which has been registered with AlarmManager, is broadcasted by the system automatically. This intent starts the target application if it is not running. It is recommended to use AlarmManager when you want your application code to be run at a specific time, even if your application is not currently running.
There is an Example.
This code set an alarm at 25/12/2012. How is possible replace 2012 with every years?
Calendar cal = Calendar.getInstance(TimeZone.getDefault(), Locale.getDefault());
cal.set(Calendar.DATE,25);
cal.set(Calendar.MONTH,Calendar.DECEMBER);
cal.set(Calendar.YEAR,2012);
Keep in mind that alarms are not persisted to disk. Since it is very likely that your device will be reset, rebooted or simply run out of battery long before the alarm is triggered, using the AlarmManager for such long periods is not a good idea. You could have a broadcast receiver for device boot (BOOT_COMPLETED) and register your alarm, but that too is not too reliable and may not be available on ICS and later (not unless the user started your app manually).
The comment above is correct though, one way to do this is for each alarm to schedule the next one.
i know AlarmManager was discussed several times, but i really can't find an answer that can help me. I have an app which needs to start a service at a certain time and make some stuff, so after some research work, i decided that AlarmManager is what i need.
I use this code to do the job
Intent myIntent=new Intent();
ComponentName cn=new ComponentName("my.package.name", "my.package.name.AlarmService");
myIntent.setComponent(cn);
PendingIntent pendingIntent= PendingIntent.getService(alarm._context, alarm.id, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) _context.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, alarm.time, pendingIntent);
Where "alarm" is an object written by me.
Now, i am sure like the death that this code works, because if i set one (or more) action for 2 minutes or hours forward, it works (i write a log on file in the first instruction of the service). If i execute
adb shell dumpsys alarm
I can see all of my pending intents.Ok, happy to see everything working, i schedule my actions at:
01:00 AM
08:00 AM
08:40 AM
09:15:AM
01:00 PM
02:00 PM
18:00 PM
after setting this actions, i execute
adb shell dumpsys alarm
and i can see all of the pending intents. Then i go to sleep and..... when i wake up in the morning at 07:30 AM, the action scheduled at 01:00 AM has not been executed and if i execute
adb shell dumpsys alarm
all of my pending intents are disappeared!!!!!!!
I am really frustrated of this behavior, because i spent a lot of time writing this application and i can't get it working properly. I'm posting this question after weeks of researchs, because i tryed every thing, but i still have this problem. Please help me
From the docs for AlarmManager (link here):
The Alarm Manager holds a CPU wake lock as long as the alarm
receiver's onReceive() method is executing. This guarantees that the
phone will not sleep until you have finished handling the broadcast.
Once onReceive() returns, the Alarm Manager releases this wake lock.
This means that the phone will in some cases sleep as soon as your
onReceive() method completes. If your alarm receiver called
Context.startService(), it is possible that the phone will sleep
before the requested service is launched. To prevent this, your
BroadcastReceiver and Service will need to implement a separate wake
lock policy to ensure that the phone continues running until the
service becomes available.
If your Service does not also hold set up proper wake locks, the device will go right back to sleep when the AlarmManager is finished, you need to manage this as well in your code.
HTH