Schedule Alarm Manager daily - java

I have some issues setting the Android AlarmManager to execute daily.
The user can set his specific time, and it should run daily on this time.
Here's the code:
#Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(MainActivity.this,
NotificationBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getService(
MainActivity.this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Make sure, there is no active Alarm
alarmManager.cancel(pendingIntent);
if(notificationEnabled) {
Calendar cal = Calendar.getInstance();
cal.set(Calendar.AM_PM, Calendar.PM);
cal.set(Calendar.HOUR_OF_DAY, notificationHour);
cal.set(Calendar.MINUTE, notificationMinute);
cal.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), AlarmManager.INTERVAL_DAY,
pendingIntent);
}
}
Now I can't figure out, why this won't work. I once set the alarm to 10:05 and the Alarm fired in the evening. The alarm should fire each day at the specific time (if it's enabled only, of course). Starting with the current day. So if i set it to a few minutes or hours later, it should run also today, and every other day, until I cancel the alarm.#
Thanks in advance

I think you get a wrong Unixtimestamp from Calender.
Try this
// 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);
From Android Trainigs Scheduling Repeating Alarms

Related

Alarm Manager is repeating more than once a day when using AlarmManager.INTERVAL_DAY

MainActivity.java
Intent intent = new Intent(this, AlarmReceiver24.class);
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY,1);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
String time= String.valueOf(calendar.getTime());
Log.i("Time:",time);
//repeat alarm every 24hours
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, alarmIntent);
AlarmReceiver class
public class AlarmReceiver24 extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
reference.child("Total").setValue(0);
Toast.makeText(context, "Total Reset", Toast.LENGTH_SHORT).show();
}
}
Android Manifest
<receiver android:name=".AlarmReceiver24"/>
I want the code to run at midnight 12:00:00 once everyday but it keeps firing again and again even after using AlarmManager.INTERVAL_DAY. I don't know what I'm doing wrong.
Each time you opens MainActivity it creates a new PendingIntent
When you start MainActivity you should remove all previously scheduled alarms.
Also
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 50);
calendar.set(Calendar.SECOND, 0);
is not midnight.
calendar.set(Calendar.HOUR_OF_DAY, 0);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
is midnight
The Alarm was getting set in past so added this condition and it was solved
if(calendar.before(Calendar.getInstance()))
calendar.add(Calendar.DATE,1);

How to send multiple broadcasts in the same day? Android/Java

I am able to send one broadcast per day, but I'm trying to send more than one per day. It seems like calling the same function over again is writing over the previous broadcast because I'm only receiving the last broadcast. here is the code
send(22,38);
send(22,39);
send(22,40);
public void send(int hour, int minute){
AlarmManager alarmMgr;
PendingIntent alarmIntent;
Context context = this;
alarmMgr = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, hour);
calendar.set(Calendar.MINUTE, minute);
alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}

Resetting data everyday on android app as an asyn task

I have an app that needs to reset its text fields and other views everyday. Its like a daily tasks that remain constant but the progress has to be reset everyday or at 12am
That a really easy task to implement using AlarmManager.
Following this guide it should be easy for you.
Example:
//define your intent
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PedingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
// Set the alarm to start at approximately 00:00 h(24h format).
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 00);
//repeteat alarm every 24hours
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
Now create a BroadcastReceiver called "AlarmReceiver" and inside it, do whatever you want to.
public class AlarmReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//do whatever you want.
}
}
Maybe you should use a WakefulBroadcastReceiver depending of your task.

Alarm Manager does not work

Alarm manager in my app does not work. I do everything like described here
https://developer.android.com/training/scheduling/alarms.html
but the alarm does not work.
Here is my code:
manifest
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
Reciever (Reciever registered in the Manifest)
#Override
public void onReceive(Context context, Intent intent) {
Intent alarmIntent = new Intent(Constants.ALARM_INTENT);
alarmIntent.setClass(context, NotificationActivity.class);
alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(alarmIntent);
}
Activity
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(context, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 1);
calendar.set(Calendar.MINUTE, 30);
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
I do not know how to make it work. Please, help me to fix it.
First, remove the SET_ALARM permission, as it is not relevant here.
Second, you need to ensure that your Calendar object is in the future. Most of the time, yours will be in the past, because you are using calendar.set(Calendar.HOUR_OF_DAY, 1) and set(Calendar.MINUTE, 30). If you execute this code after 01:30, it will be in the past. In that case, you need to add() one day to make it be tomorrow at 01:30.
Third, use adb shell dumpsys alarm to see if your alarm is scheduled, once you make the above fixes.
Fourth, use LogCat to see if there are any warnings or errors coming from your code, such as perhaps AlarmReceiver or NotificationActivity not being registered in the manifest.
You're asking for an alarm based on the system's elapsed realtime clock here:
alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
But you're passing it a time based on a normal calendar clock. If you want an alarm to happen using a calendar-based clock, say this instead:
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,
calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Note the differnce in javadoc between ELAPSED_REALTIME_WAKEUP and RTC_WAKEUP.

Set time for Alarm Manager to Stop (Android)

I have a sound that is scheduled to play at 9 pm every day using the alarm manager class in android. How can I set a time for it to stop (for example, have it start at 2 pm and stop at 11 pm)?
void playAudio() {
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.set(Calendar.HOUR_OF_DAY, 21);
AlarmManager alarmMgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent objIntent = new Intent(this, Sound.class);
PendingIntent alarmIntent = PendingIntent
.getService(getApplicationContext(), 0, objIntent,
PendingIntent.FLAG_ONE_SHOT);
alarmMgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, alarmIntent);
}

Categories