Broadcast receiver doesnt broadcast in given time - android

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.

Related

AlarmManager does not set alarm at 2 am daily

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?

Call AlarmManager when phone turned on after new day

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());
}
}
}

Why is my Android AlarmManager code not working properly?

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)

Broadcastreceiver triggered at Start

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);

android repeat alarm for every monday or every tuesday

I am developing an alarm based application, in which i have to repeat the alarm for every weekday like (every monday, tuesday, wednesday) based on user input. I used this snippet
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP, alarmToSetInMilliSeconds, sender);
If user selects every monday means, i found the milliseconds more to next monday date and i set an alarm, and its working fine, how can i repeat it for other next monday, I want some idea to achieve it, any help are appreciated. Thanks.
here is my code to set alarm to repeat every month in 4th
public void init(Context context) {
Intent intent = new Intent(context, AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Get the AlarmManager service
alarmManager = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
}
public void createAlarmControler(Context context) {
init(context);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, 4);
cal.set(Calendar.HOUR_OF_DAY, 10);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
Date now = new Date(System.currentTimeMillis());
if (cal.getTime().before(now)) {
cal.add(Calendar.MONTH, 1);
}
long firstTime = cal.getTime().getTime();
alarmManager.cancel(pendingIntent);
Log.e("", "firstTime: " + firstTime);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, firstTime, 1000 * 60
* 60 * 24 * 30, pendingIntent);
}
public void cancelAlarmControler(Context context) {
init(context);
alarmManager.cancel(pendingIntent);
}

Categories

Resources