I set clock alarm for midnight. But setting the Alarm triggers my Broadcast receiver?
I set the Alarm at the App start.
the code:
//set the alarm
protected void onCreate(Bundle savedInstanceState) {
as=new AlarmSetter();
as.SetAlarma(this); .........
//the AlarmSetter
public class AlarmSetter {
public void SetAlarma(Context context){
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);
Intent intent = new Intent(context, AlarmKicked.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 1333333, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am =(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
//and my Broadcast reciever
public class AlarmKicked extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
NotificationsA.Notizer(context);
System.out.println("IT IS MIDNIGHT! SETTING TIMER..");
}
}
//Manifest
<receiver android:name=".AlarmKicked" android:process=":remote"/>
When I start the App, I set the Allarm.. but in the same time I get in the Logcat : IT IS MIDNIGHT! SETTING TIMER..
What is triggering my Broadcastreciever at the Start?
EDIT:
Works!:
public void SetAlarma(Context context){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 0);
calendar.add(Calendar.DAY_OF_MONTH, 1);
calendar.set(Calendar.AM_PM, Calendar.AM);
You are setting for midnight, however your Calendar's time is now midnight of the present day, which has already passed.
This causes the Alarm to go off as soon as it's registered.
Quoting from the documenatiation:
If the time occurs in the past, the alarm will be triggered immediately, with an alarm count depending on how far in the past the trigger time is relative to the repeat interval.
Instead, add a day to your calendar, that should stop it from going off.
Calendar calendar = Calendar.getInstance();
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.DATE, 1);
Related
I want the AlarmManager to set an alarm at 2 am daily... So below setAlarm is called only the first time someone opens the apps. I have added alarmManager.setRepeating so that it occurs daily.
public class AlarmUtil {
public static void setAlarm(Context context) {
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
long start = System.currentTimeMillis();
Intent in = new Intent("ALARM_RECEIVER");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, in, PendingIntent.FLAG_CANCEL_CURRENT);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.SECOND, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.HOUR, 2);
calendar.set(Calendar.AM_PM, Calendar.AM);
DateFormat format = new SimpleDateFormat("MMMM dd , yyyy HH:mm:ss", Locale.ENGLISH);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi);
}
}
}
The below class is what should get called at 2 am daily irrespective of whether app is active or inactive.
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
context.startService(new Intent(context, AlarmService.class));
}
But my problem is the above AlarmReceiver gets called every 30 seconds or somewhat occurs randomly, but occurs many times, whereas I want it just to get called once a day. I don't understand where am I going wrong?
I have an AlarmManager that I call at the end of every day, ie 23.59PM, to do some stuff for the new day. (Its called, NewDayReceiver.class.) It repeats every day.
I also set a broadcast receiver that will run the alarm on reboot. It all works fine.
However, if the phone is off at the end of the day, the alarm for that day is not called. When the phone is turned back on, on boot the alarm is set again, however its set for the end of the new day. It comes out, that the alarm for that day was never called. For example, if its off Sunday night, on Monday morning the phone is turned on but the alarm hasn't been called. It will only be called Monday night?
How can I do it, that on the boot broadcast receiver, I can check if the alarm was called for that day, and if it wasn't set the alarm to go off right away.
I hope its clear.
Here is my alarm for every day.
Intent intent = new Intent(this, NewDayReceiver.class);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if(!calendar.before(Calendar.getInstance())) {
// Repeat every 24 hours
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent);
Log.d(TAG, "New day alarm set for:" + calendar.getTime() + " and will repeat every day");
}
Here is my BroadcastReceiver, that I call on boot:
public class BootBroadcastReceiver extends BroadcastReceiver {
private static final String TAG = "999.BootBroadcastReceiver";
#Override
public void onReceive(Context pContext, Intent intent) {
newDayAlarm(pContext);
newWeekAlarm(pContext);
}
private void newWeekAlarm(Context context) {
Intent intent = new Intent(context, NewWeekReceiver.class);
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 55, intent, 0);
// real for end of week
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
calendar.set(Calendar.HOUR_OF_DAY, 23);
calendar.set(Calendar.MINUTE, 59);
calendar.set(Calendar.SECOND, 59);
if(!calendar.before(Calendar.getInstance())) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Log.d(TAG, "New week alarm set for:" + calendar.getTime());
}
}
}
I have an alarm in my app:
public class MainActivity extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState){
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.curentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(MINUTE, 11);
Intent intent = new Intent(this, AlarmReceiver.class);
PendingIntent pendingIntent = Pending.getBroadcast(this, 0, intent, 0);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntente);
}
}
This is my AlarmReceiver.class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent){
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss");
System.out.println("ALARM: " + sdf.format(new Date()));
}
}
And in my AndroidManifest.xml I have this line
<receiver android:name=".main.AlarmReceiver" />
The issue is that the alarm does not alarm for the time I set. I set the alarm for
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(MINUTE, 5);
Then the alarm alarmed at 11:11:29. Then I set the alarm for
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(MINUTE, 20);
Then the alarm alarmed at 11:26:29. Then I set the alarm for
calendar.set(Calendar.HOUR_OF_DAY, 11);
calendar.set(MINUTE, 29);
Then the alarm alarmed at 11:41:29.
It continued this way all morning. However, if I replace this line
alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntente);
with this line
alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, (SystemClock.elapsedRealtime()+10000), pendingIntent);
Then the alarm alarms perfectly in the appropriate 10 seconds.
What is going on?
As per the documentation, setInexactRepeating is not accurate by design. In order to reduce the battery drain by waking up the device too many times, it internally tries to fire several alarms at the same time resulting in inexact alarm time for some of the clients.
https://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int,%20long,%20long,%20android.app.PendingIntent)
This is my SetAlarm.class. here i set time and date according to calendar.
Calendar date = Calendar.getInstance();
date.set(Calendar.YEAR, 2016);
date.set(Calendar.MONTH, 1);
date.set(Calendar.DAY_OF_MONTH, 29);
date.set(Calendar.HOUR_OF_DAY, 2);
date.set(Calendar.MINUTE, 43);
date.set(Calendar.SECOND, 0);
date.set(Calendar.AM_PM, Calendar.PM);
Intent myIntent = new Intent(SetAlarm.this, MyReceiver.class);
pendingIntent = PendingIntent.getBroadcast(SetAlarm.this, 0, myIntent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, date.getTimeInMillis(), pendingIntent);
And this is my receiver class where toast is to be shown.
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Birthday Set", Toast.LENGTH_LONG).show();
}
The Calendar months are zero-based: January=0, February=1. The date you show in your code is for February 29th. Also, Calendar.HOUR_OF_DAY is used for the 24 hour clock. For example, to specify 10PM set(Calendar.HOUR_OF_DAY, 22). You should not use HOUR_OF_DAY and AM_PM together. When using AM_PM, set the hour with HOUR, not HOUR_OF_DAY.
I am using Alarm manager to run alarm at specific time every day. Below is the code
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(this, OnAlarmReceive.class);
PendingIntent pendingIntent =PendingIntent.getBroadcast(this.getApplicationContext(), 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
24*60*60*1000, pendingIntent);
I am Setting alarm at 12AM every day. And Below is the code for BroadCastReciever
#Override
public void onReceive(Context context, Intent intent)
{
System.out.println("Time is 12 Am");
Toast.makeText(context, "Alarm Triggered", Toast.LENGTH_LONG).show();
}
Problem in this code is Alarm is Triggered As soon as i Run the Application Irrespective of time. Any help will be Appreciated. Thank You
The alarm will only fire immediately if you set the alarm in the past.
E.g. it is now 10:00h and you want to set an alarm every day at 09:00. To avoid this, you have to look what time it is now, and shift the alarm 1 day if that is the case... This allows you to use the setRepeating method (which is more precise than setInexactRepeating)
This fixes the issue:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
calendar.set(Calendar.MINUTE, 00);
if(Calendar.getInstance().after(calendar)){
// Move to tomorrow
calendar.add(Calendar.DATE, 1);
}
//... continue like before by setting the alarm
I had the same issue as you and I couldn't get it to work. Plus all the examples I could find were also setting a specific date, not only just a time. This should work for you:
Intent myIntent = new Intent(ThisActivity.this , NotifyService.class);
AlarmManager alarmManager = (AlarmManager) Context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent = PendingIntent.getService(ThisActivity.this, 0, myIntent, 0);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 12);
calendar.set(Calendar.MINUTE, 00);
calendar.set(Calendar.SECOND, 00);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 24*60*60*1000, pendingIntent); //Repeat every 24 hours
Hope this helps you fix your problem!
try to use this:
alarmManager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime() + AlarmManager.INTERVAL_DAY,
AlarmManager.INTERVAL_DAY, intent);
You could use something like this:
Calendar cal = Calendar.getInstance();
if(cal.getTimeInMillis() < System.currentTimeMillis()) {
cal.add(Calendar.DAY_OF_YEAR, 7);
}