I am working on Android AlarmManager and i found one issue in my code. The Alarm manager gets called in different time as well , i don't find what's the reason , i gave alarm at 7:10 AM but it gets called whenever i open the app in different time like 3:10 PM , 4:30 PM etc.
MyCode :
public void setAlarm(){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR));
calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH));
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1);
calendar.set(Calendar.HOUR_OF_DAY, 7);
calendar.set(Calendar.MINUTE, 10);
calendar.set(Calendar.AM_PM,Calendar.AM);
Intent intent = new Intent(appContext, MyAlarmManager.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(appContext, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager) appContext.getSystemService(Context.ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
}
MyAlarmManager.java
public class MyAlarmManagerextends BroadcastReceiver {
#Override
public void onReceive(final Context context, Intent intent) {
setAlarm();
} }
So , this is the above code , please let me know what's wrong in the code and suggest me some solution.
This is happening because the alarm is set in the past (when you set it anytime after 07:10 AM) So, you have to add 1 day if it is past 07:10 AM today.
Explanation -
Suppose it is 8 AM today and you are setting an alarm for 07:10, it sets an alarm for the same day 07:10 AM which is already past, hence it goes off whenever you open the app
Solution -
Option 1 - Either set date, month and year too like -
calendar.set(Calendar.DATE, date);
calendar.set(Calendar.MONTH, month);
Option 2 - Check what is the current time, if current time > the alarm time, add one day to the alarm
//If you are past 07:10 AM then
calendar.add(Calendar.DATE,1);
This might happen if you are using Android version Marshmallow or higher. It is called Doze Mode
Doze Mode is implemented to save the battery by delaying stuff like AlarmManager, SyncAdapter, JobScheduler. Refer to this link to find out more about Doze Mode
Related
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 want to start alarm when i have selecting the days,hours,and minutes.
This is my code so far :
public void startAt() {
Intent alarmIntent = new Intent(backgroundApplication.this, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(backgroundApplication.this, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.DAY_OF_WEEK,calendar.WEDNESDAY);
calendar.set(Calendar.HOUR_OF_DAY,7);
calendar.set(Calendar.MINUTE,35);
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
1000 * 5, pendingIntent);
}
For the example i set the DAY_OF_WEEK is today(friday) it works great. But when i changed it into Wednesday, or other day before friday it still running.
Is there any other way to start in selected days ?
According to the documentation http://developer.android.com/reference/android/app/AlarmManager.html#setRepeating%28int,%20long,%20long,%20android.app.PendingIntent%29, the 2nd parameter is the trigger time in milliseconds and if you set it to a time in the past, the alarm will trigger immediately. If you want the alarm to only start from next Wednesday, then you have to advance your Calendar time to the next Wednesday with calendar.add(Calendar.WEEK_OF_MONTH, 1). By the way, in your code, you don't need to have calendar.setTimeInMillis(System.currentTimeMillis()).Calendar.getInstance() will return the current time by default. You should also use static field Calendar.WEDNESDAY.
I have this static helper function, to set a daily-repeating alarm:
public static void setAlarm(Context context, int hh, int mm)
{
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hh);
calendar.set(Calendar.MINUTE, mm);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intentAlarm = new Intent(context, AlarmReciever.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intentAlarm, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, alarmIntent);
}
I have three places where this helper function is called:
When device boots up, I want to re-register the alarm:
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
setAlarm(context, 7, 00);
}
}
In one of my activities, call it ActivityA:
AlarmReciever.setAlarm(ActivityA.this, 7, 00);
In another activity, call it ActivityB:
AlarmReciever.setAlarm(ActivityB.this, 7, 00);
Questions:
What would happen if all three kinds of setAlarm() are called? I'm concerned that they don't have the same context. Would android know they are the same PendingIntent and only fire once? I want it to only fire once at 7 am.
What if I press setAlarm button in ActivityA a few times so the same alarm is set with the same context and intent, when 7 am comes, how many would fire?
Say it fired at 7 am this morning and now it's 2 pm in the afternoon. I now press the button in ActivityA. The alarm would fire instantly (tested), because 7 am is a "past time". But before adding the new alarm, shouldn't the system check existing ones, and realize this is a repeating intent, and one has been fired at 7 am already? In my opinion it should ignore my setAlarm() requested at 2 pm.
For what it's worth, I did experiments and I'm posting the results here in case anyone else wonders:
As long as the PendingIntent is identical, android will only fire the alarm once. You may set the alarm however many times you want, from whatever activities/classes you want.
One.
The new alarm will just trigger right way (because 7 am is a "past time"). You can write code to record the last fired date/hour (e.g. use SharedPreferences), and do some logic there, or depends on your situation, you may want to schedule the alarm to fire at 7 am the next day (do a plus one day on the Calendar object).
I'm trying to make a simple alarm clock and everything is working as it should, but I'm finding that the firing of the alarm is delayed.
I think the problem may be that the system is recording the exact millisecond I am setting the alarm, and then firing the alarm at that exact millisecond. So for example, if I make an alarm for 8:00am but I set the alarm at 10:00:30.225pm, then the alarm will fire at 8:00:30.225am.
How would I go about making sure the alarm fires exactly on the minute? I tried using setExact(...) but that just caused my app to crash.
Thanks in advance!
public void setAlarm(View view) {
TimePicker timePick = (TimePicker) findViewById(R.id.time_pick);
Calendar calendar = Calendar.getInstance();
//Set alarm based on values in time picker
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, timePick.getCurrentHour());
calendar.set(Calendar.MINUTE, timePick.getCurrentMinute());
setAlarmDisplay(calendar); //shows time in EEE, MMM d yyyy hh:mm aa format
Intent intent = new Intent(this, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE));
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmIntent);
Toast.makeText(view.getContext(), "Alarm set!", Toast.LENGTH_LONG).show();
}
I'd rather comment this but I don't have 50 rep yet so I'll have to submit an answer.
I think the problem is the line
calendar.setTimeInMillis.
You then set the hour and minute but everything else (seconds and ms) stay exactly as it was when you ran that code.
If you want the alarm to go off at exactly 10:30, set the hours, set the minutes and then reset the seconds and milliseconds to be 0;
I want the following:
The AlarmManager sends a daily intent to my AppWidgetProvider at midnight EXACTLY.
The Alarm must wake up the device.
Here is how I did it:
In my AppWidgetProvider subclass:
#Override
public void onEnabled(Context context) {
super.onEnabled(context);
Log.d(LOG_TAG, "Widget Provider enabled. Starting timer");
//Setting the Calender object to midnight time.
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 0);
calendar.add(Calendar.MINUTE, 0);
calendar.add(Calendar.HOUR, 0);
//The fired Intent
Intent intent = new Intent(CLOCK_WIDGET_UPDATE); //custom intent name
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), 1000*60*60*24, pendingIntent);
}
Although I think I did it right, this code doesn't work!
To test it, I changed 1000*60*60*24 with 10000 (10 sec) and updated a textView in the widget with random number. The random number never changed.
Any help will be appreciated !
I finally found the problem !
It seems that I need to set the AM_PM value too. When I print the calender using getTime().toLocaleString(), it was set to the next day but 12 hours later (12:00:00 PM).
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.set(Calendar.AM_PM, Calendar.AM);
calendar.add(Calendar.DAY_OF_MONTH, 1);
This is a perfect setup for the Calender instance that will set the alarm to the next day at midnight (i.e. the nearest midnight). You just pass it to the setRepeating() method.
Thanks #CommonsWare
First, what you want is not strictly possible. Android is not a RTOS; you may not get control "at midnight EXACTLY"
Second, your Calendar object represents a time in the past (unless it happens to be midnight at the time you are executing that code, in which case it represents the present). Try a start time in the future.