I am following Android developers documentation and some other tuts to create an Alarm manager which fires and wakes up the CPU at 4pm everyday, following is my code:
private AlarmManager alarmMgr;
private PendingIntent alarmIntent;
BroadcastReceiver br;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}
public void setup() {
br = new BroadcastReceiver() {
#Override
public void onReceive(Context c, Intent i) {
Toast.makeText(c, "Rise and Shine!", Toast.LENGTH_LONG).show();
//Invoke the service here Put the wake lock and initiate bind service
}
};
registerReceiver(br, new IntentFilter("com.testrtc") );
alarmIntent = PendingIntent.getBroadcast( this, 0, new Intent("com.testrtc"),
0 );
alarmMgr = (AlarmManager)(this.getSystemService( Context.ALARM_SERVICE ));
}
}
Manifest:
<uses-permission android:name="android.permission.WAKE_LOCK" />
However I dont get any errors, but the alarm (Toast message) wont fire.
EDIT from the developer docs:
RTC examples
Here are some examples of using RTC_WAKEUP.
Wake up the device to fire the alarm at approximately 2:00 p.m., and repeat once a day at the same time:
// Set the alarm to start at approximately 2:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 14);
// With setInexactRepeating(), you have to use one of the AlarmManager interval
// constants--in this case, AlarmManager.INTERVAL_DAY.
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
This one for set Repeating, says that if I want my alarm to fire at 8:30 and then repeat after each 20 minutes, however I just want to fire my alarm at a specific time but I dont want to repeat it.
Wake up the device to fire the alarm at precisely 8:30 a.m., and every 20 minutes thereafter:
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);
Decide how precise your alarm needs to be
Choosing the alarm type is often the first step in creating an alarm. A further distinction is how precise you need your alarm to be. For most apps, setInexactRepeating() is the right choice. When you use this method, Android synchronizes multiple inexact repeating alarms and fires them at the same time. This reduces the drain on the battery.
For the rare app that has rigid time requirements like yours, the alarm needs to fire precisely at 4:00 p.m. everyday then use setRepeating().
Reference: Decide how precise your alarm needs to be
Solution :
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
// Set the alarm to start at approximately 4:00 p.m.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 16);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000*60*60*24, alarmIntent);
Edited Testing : (Fire alarm at every 10seconds)
alarmMgr = (AlarmManager)getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime(),
1000*10, alarmIntent);
Conclusion :
setup() method was not called before dealing with alarms.
Update for API 19+
setRepeating is inexact when targeting api level 19 or higher. For exact repating you can now use setExact() and manage repeating yourself.
reference: AlarmManager documentation
Instead of using setInexactRepeating() I suggest you to use setRepeating()
from the docs,
setInexactRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm that has inexact trigger time requirements; for example, an alarm that repeats every hour, but not necessarily at the top of every hour.
setRepeating(int type, long triggerAtMillis, long intervalMillis, PendingIntent operation)
Schedule a repeating alarm.
Documentation is quite clear:
public void setInexactRepeating (...)
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.
so to my understanding but there may be a delay of almost an entire alarm interval means you may have one day delay, because u use AlarmManager.INTERVAL_DAY.
set minutes and seconds also to calendar ..
calendar.set(Calendar.HOUR, 16); // At the hour you wanna fire
calendar.set(Calendar.MINUTE, 0); // Particular minute
calendar.set(Calendar.SECOND, 0);
and use
alarmManager.setRepeating(AlarmManager.RTC,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
alarmIntent);
I had your same problem (usually is an Huawei issue) and I resolved by enabling the app in the PowerManager or Protected Apps.
Try to create a receiver that extends WakefulBroadcastReceiver:
public class MyReceiver extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Intent myService = new Intent(context, MyService.class);
context.startService(myService);
}
}
In your service, you can put your toast, or try to write a log in a file to be sure that it works. Then, in your activity:
Intent myAlarm = new Intent(context.getApplicationContext(), MyReceiver.class);
PendingIntent recurringAlarm = PendingIntent.getBroadcast(context.getApplicationContext(), 0, myAlarm, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
alarms.setRepeating(AlarmManager.RTC_WAKEUP, Calendar.getInstance().getTimeInMillis(), 4 * 1000 * 60, recurringAlarm);
// Alarm every 4 minutes
Related
Below is the code I am using to create alarms when data is pulled from external API. If the time set is in the past, the alarm goes off as soon as it is set(2 second gap). For example if I set the alarm for 8:00 AM, 10th April at 10:00 AM on 11th April(Past time). It starts the alarm as soon as it is set.
public static final int ALARM_REQUEST_CODE = 1001;
public static AlarmManager alarmManager = (AlarmManager) EHCApplication.getInstance().getApplicationContext().getSystemService(Context.ALARM_SERVICE);
public static Intent alarmIntent = new Intent(EHCApplication.getInstance().getApplicationContext(), AlarmReceiver.class);
public static PendingIntent pendingIntent = PendingIntent.getBroadcast(EHCApplication.getInstance().getApplicationContext(), ALARM_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
public static void setAlarm(Reminder rm) {
for (ScheduledTime time : rm.getScheduledTime()) {
Bundle bundle = new Bundle();
bundle.putParcelable(Constants.ARGS_SELECTED_MEDICINE, medicine);
alarmIntent.putExtras(bundle);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time.getTimeInMilliseconds(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
}
I am expecting the alarm to go off from next time it hit the time. Where am I making mistake?
This is the expected behavior.
From the documentation of setRepeating() (and other AlarmManager set methods):
If the stated trigger time is in the past, the alarm will be triggered
immediately
If you would like to prevent that happening, then simply do not set alarms with a past trigger time (e.g. check against System.currentTimeMillis() when setting the alarm).
Well, I ran into same problem and after studying I found that alarm will be run as soon when past time is set for the alarm.
Source: Here is documentation of Alarm Manager - setRepeating()
So, I resolved the issue by checking if "Calendar time is in past from system time than I add a day"
Working code:
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, min);
calendar.set(Calendar.SECOND, 0);
alarmManager.cancel(pendingIntent);
// Check if the Calendar time is in the past
if (calendar.getTimeInMillis() < System.currentTimeMillis()) {
Log.e("setAlarm","time is in past");
calendar.add(Calendar.DAY_OF_YEAR, 1); // it will tell to run to next day
}
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, id, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent); //Repeat every 24 hours
If I call the setinexactrepeating() alarm method in the onCreate , how does it affect the alarm which is being repeated at the specified interval
Here is the code of setting alarm, I am calling this method in the onCreate()
public void setAlarm()
{
Intent myintent=new Intent(this,AlarmReciever.class);
Random random = new Random();
int ran = random.nextInt(total_words.size());
String tempString=onlySearch(total_words.get(ran), 1);
myintent.putExtra("word", total_words.get(ran));
myintent.putExtra("meaning", tempString);
myintent.putExtra("language", 1);
alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
PendingIntent pendingIntent=PendingIntent.getBroadcast(this,101,myintent,PendingIntent.FLAG_UPDATE_CURRENT);
//NotificationTime is the sharedPreference file, in which i am storing hours and minute got from timepicker
alarmManager.cancel(pendingIntent);
Calendar calendar=Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, NotificationTime.getInt("hour", 12));
calendar.set(Calendar.MINUTE, NotificationTime.getInt("min", 0));
calendar.set(Calendar.SECOND, 0);
if(calendar.before(Calendar.getInstance()))
calendar.add(Calendar.DATE,1);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY
, pendingIntent);
System.out.println("ALARM SET STATUS");
}
Alarm is first scheduling at the exact time perfectly but it does not gets repeated.
Actually I wanted to show notification daily at the specified time and I am passing a String with the intent. By default I had set to 12:00 PM but user can change its time. The first notification after setting time (or the first alarm) is working but its repetition is not working.
Please have a look, and if someone knows a better solution to show notification daily at the same specified time it would be appreciated if you tell me.
Thanks in advance
user setRepeating for Repeating Alarm
AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
/* Repeating on every interval */
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
I have 2 alarms set, one for notifications, and the other one to do some tasks. My problem is that only one alarm seems to work( the notifications service one, the first alarm set). The Other alarm never goes off. Here is my code :
Intent myIntent1 = new Intent(getApplicationContext(), NotificationService.class);
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 0, myIntent1, 0);
AlarmManager alarmManager1 = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTimeInMillis(System.currentTimeMillis());
long frequency1 = 30 * 1000; // in ms
alarmManager1.setRepeating(AlarmManager.RTC_WAKEUP, calendar1.getTimeInMillis(), frequency1, pendingIntent);
// Set alarm to fire go to Next day everyday at the same time
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 57);
calendar.setTimeInMillis(System.currentTimeMillis());
Intent myintent = new Intent(getApplicationContext(), AlarmNextDayService.class);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pi = PendingIntent.getService(getApplicationContext(), 11, myintent,0 );
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pi);
Any suggestions are welcome. I have looked atother sources as well nothing works for me till now.
I have also added alarm permisison in the manifest file as the following :
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Thank you
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
calendar.set(Calendar.MINUTE, 57);
calendar.setTimeInMillis(System.currentTimeMillis());
You are setting the HOUR_OF_DAY and the MINUTE but them you override that by calling setTimeInMillis(System.currentTimeMillis());
After that you set the alarm with the calendar.getTimeMillis() value which is already in the past, so the alarm is cancelled I think.
You're most likely seeing an issue because a Service is not guaranteed to run when triggered by an alarm, due to power save. If your device is on battery and idle when that alarm goes off, it will not trigger until the next time the device is full on or on AC power. You'll need to use a BroadcastReceiver which holds a wake lock which is then released by the Service when it is done. The WakefulBroadcastReceiver makes this a little easier to handle. This article will help provide more details.
Here is the code that I used to set an alarm for my widget:
private static void setAlarm(Context context) {
Intent myIntent = new Intent(context, Widget.class);
myIntent.setAction(AUTO_UPDATE);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 8);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
pendingIntent);
}
but the problem is that even in the sleep mode, onReceive() is still triggered by the intent.
Although after using setInexactRepeating instead of setRepeating, the delays between calls get increased up to 1 minute in sleep mode, but that's still battery consuming.
I believe you are setting the alarm to trigger 8 seconds after the Calendar's time, which you have set 8 seconds ahead of the current time .. so you are setting the alarm to trigger instantly.
I don't see any reason you need a Calendar here. The calendar is just used to track the time 8 seconds in the future here:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 8);
The alarm is created to trigger every 8 seconds here:
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
pendingIntent);
The alarm continues to trigger every eight seconds.
I would try changing:
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
pendingIntent);
To:
alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), 8000,
pendingIntent);
If you continue having problems, then maybe the interval is the problem. Try changing setRepeating() to set() to see if that's the case.
i created alarm demo . In that demo i am repeating an alarm . I have one problem in my demo . My alarm called service even if time passed . I am setting 16:08:00 time and called that alarm so it called my alarm service after passed that time.Please help me to stop this criteria.
AlarmManager alarmManager = (AlarmManager)ctx.getSystemService(ctx.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 16);
calendar.set(Calendar.MINUTE, 8);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = createPendingIntent(ctx);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);
CreatePendingIntent Method
private static PendingIntent createPendingIntent(Context context)
{
Intent myIntent = new Intent(context, MyAlarmService.class);
return PendingIntent.getService(context,0, myIntent, 0);
}
When setting an alarm to past time, the alarm immediately pops up.
Simply check if the current time is bigger than the alarm time. If so, add 24 hours to the alarm time and set the alarm.:
long timeToAlarm = calendar.getTimeInMillis();
if (calendar.getTimeInMillis() < System.currentTimeMillis())
{
timeToAlarm += (24*60*60*1000);
}
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, timeToAlarm, 24*60*60*1000, pi);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pi);
if you used the above code the alarm repeats at the 24*60*60*1000 interval time.If you don't want to repeat the alarm then use the bellow code
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pi);
the above code will cal the alarm only onetime.