How to delay Trigger(starting time) of repeating alarm? - java

The problem is that I want to delay the repeating-alarm's first fire.
for example I want this pending intent work after 10 mins of the click not right away, how to do that?
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Main.this, ReportService.class);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar c = Calendar.getInstance();
PendingIntent pendingIntent = PendingIntent.getService(Main.this, 0, intent, 0);
alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), 112500, pendingIntent);
}
the 2nd paramater in this setInexactRepeating called triggerAtMillis
From Android developer guide:
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.
Iam changing it and it always start on the click and does no delay, any help?

Check the
developer link here. The second parameter is triggerAtMillis. It used as time in milliseconds that the alarm should first go off
So in your case, add the extra milliseconds to c.getTimeInMillis(), after which you want to first run your pending intent. :)

check into handler.postDelayed function where you can specify a delay

If using the ELAPSED_REALTIME clock, do this
manager.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
SystemClock.elapsedRealtime()+180000,
180000,
pendingIntent);
Do not use zero or System.currentTimeMillis() as an offset for the ELAPSED clock.

Related

How do I create an alarm clock like Alertmanager?

The alarm clock must start by itself, even if the app has been closed and the smartphone restarts.
Is there a safe way to program such an alarm clock?
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent startServiceIntent = new Intent(context, MyService.class);
PendingIntent startServicePendingIntent =
PendingIntent.getService(context,0,startServiceIntent,0);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
if(alarmManager != null)
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()
+ 1000 * 60,1000 * 30,startServicePendingIntent);
you can do it easily but you need add some Property to your app like :
1-set first method for set alarm
2-save and handle value for alarm (Room ، Sql or etc) // if you want make loop alarm
3-use broadcastreceiver for two reason(set notification or handle background and close app or restart or somethings like that)
4- some custom method for better handle data streaming in app
i hope this link will can help you
link refrences
have fun :)

Android alarm manager fire at the exact moment

I have an app which needs to do task at the exact time provided just like the stock android alarm clock I have used setExactAndAllowWhileIdle() / setExact(). But BroadcastReciever fires after some minutes and sometimes after opening the app, is there any workaround for it?
Alarm Setter code
// set Alarm
Intent intent = new Intent(this.context, AlarmReceiver.class);
intent.putExtra("alarmInfo", extraInfo);
intent.setAction(Long.toString(System.currentTimeMillis()));
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.context, 123456, intent, 0);
AlarmManager alarmManager = (AlarmManager) this.context.getSystemService(Context.ALARM_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, finalTime - 400, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, finalTime - 400, pendingIntent);
}
BroadcastReciever
setNextAlarm();
Log.d("RESULT", "fired");
Intent alarmIntent = new Intent(context, AlarmSoundService.class);
// Log.d("extra", "alarmInfo: " + intent.getStringExtra("alarmInfo"));
alarmIntent.putExtra("alarmInfo", intent.getStringExtra("alarmInfo"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(alarmIntent);
} else {
context.startService(alarmIntent);
}
setNextAlarm() is the previous code I provided which sets the alarm for the future task.
EDIT
Ok so I have solved my problem the first is getting my alarm to fire at the exact moment so setExactAndAllowWhileIdle() / setExact() are both kind of exact so if I need to show user something and have to be accurate
I used the setAlarmClock() as the android doc explains
The system may also do some prep-work when it sees that such an alarm
coming up, to reduce the amount of background work that could
happen if this causes the device to fully wake up
The second problem was that the alarm wasn't firing up unless I open the app this was due to the fact that two alarms were firing between the interval of 5 minutes and since I was using setExactAndAllowWhileIdle()
and the android doc explains
Under normal system operation, it will not dispatch these alarms more
than about every minute (at which point every such pending alarm is
dispatched); when in low-power idle modes this duration may be
significantly longer, such as 15 minutes.
And that was it.

An application which adds +5 every 24 hours

I am working on an app which counts +5 every 24 hours, but time on device should not influence this timer. For instance, if it is 9:00 AM on the clock and 20 minutes on the timer, and user sets device time to 9:15, timer should still have 20 minutes to count.
You can use the android.net.SntpClient which is a simple SNTP client for retrieving the time from a SNTP service:
SntpClient client = new SntpClient();
if (client.requestTime("time.foo.com")) {
long now = client.getNtpTime() + SystemClock.elapsedRealtime() - client.getNtpTimeReference();
}
You can find the implementation here.
There are two general clock types for alarms: "elapsed real time" and "real time clock" (RTC). Elapsed real time uses the "time since system boot" as a reference, and real time clock uses UTC (wall clock) time. This means that elapsed real time is suited to setting an alarm based on the passage of time (for example, an alarm that fires every 30 seconds) since it isn't affected by time zone/locale. The real time clock type is better suited for alarms that are dependent on current locale.
You can use AlarmManager with RTC as following
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,DAILY_REMINDER_REQUEST_CODE, yourIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
am.setInexactRepeating(AlarmManager.RTC_WAKEUP, setcalendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pendingIntent);
And Then use a BroadcastReciever to get the event and do your stuff :
public class AlarmReceiver extends BroadcastReceiver {
String TAG = "AlarmReceiver";
#Override
public void onReceive(Context context, Intent intent) {
//Your logic here
}
}

AlarmManager Android SDK

I have a problem executing the following code:
Intent myIntent = new Intent(MedicineEntry.this, DisplaySchedule.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MedicineEntry.this, 0, myIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 17);
calendar.set(Calendar.MINUTE, 25);
calendar.set(Calendar.SECOND, 0);
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 1);
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(MedicineEntry.this, "Alarm Will Start In A Second", Toast.LENGTH_LONG).show();
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_FIFTEEN_MINUTES, pendingIntent);
To elucidate my problem, the app should fire up according to the values set for HOUR_OF_DAY, MINUTE and SECOND - even if the device is in sleep mode. I've tried testing the code, but the app does not seem to work. I've tried replacing AlarmManager.INTERVAL_FIFTEEN_MINUTES with 5*1000 which worked fine. Please go easy with the terminologies. I've just started to program with Android this semester at college.
EDIT: This app I am creating is a 'reminder' app. The user is expected to enter the time which the app should execute.
There are basically two ways of interacting with the AlarmManager. Your post is somewhat vague about which will suit your needs.
Anyway, if you want an "Do every 15 minutes" type of alarm, use the ELAPSED mode:
long firstActivation = SystemClock.elapsedRealtime(); // and not System.currentTimeMillis()
mgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, firstActivation, INTERVAL_MILLISECS, intent);
But, if you're looking for an "Do every day at 15:30" semantics, use RTC mode, like this:
long activationTime = calendar.getTimeInMillis();
long TWENTY_FOUR_HRS = 24 * 60 * 60* 1000;
mgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, activationTime, TWENTY_FOUR_HRS, intent);
Two things to note at the above code:
A. I am using a waking up type of alarms. These are potentially expensive as they would wake up a sleeping device. Are you sure this is what you need (hint: most apps do not)?
B. I am using setInexactRepeating() instead of setRepeating() so to allow the alarm manager to batch together several alarms thus saving resources. If you do not have a good reason not to do so, stick to using setInexactRepeating()

AlarmManager repeating constantly once triggered

I need my Service to do tasks every night at 02:00, and if failed in task then schedule itself to start again after 30min, but if succeeded then stop itself and start again the other night at 02:00. I managed to get it start at 02:00 but after it is triggered it just triggeres itself continiously.
My code is in Service, and here is the code itself:
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
//doTasks();
MediaPlayer player = MediaPlayer.create(this,
Settings.System.DEFAULT_NOTIFICATION_URI);
player.start();
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 2);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),AlarmManager.INTERVAL_DAY, pendingIntent);
}
doTasks() is the tasks I need to run but because they are slow I use notification sound for testing purposes. And pendingIntent is this Service itself.
Any ideas what is it that makes my AlarmManager trigger constantly?
If your pendingIntent is set to start the Service then basically you're repeatedly creating a new alarm each time in the onStart(...) method.
AlarmManager has a mechanism where it will trigger an alarm "late" if the device was asleep when it should have triggered. In your case, because you constantly re-create the alarm, AlarmManager constantly thinks it should be triggered immediately even if the time is past 02:00:00.
Create the alarm somewhere else to prevent it being constantly re-created each time the Service starts.

Categories