Android alarm manager fire at the exact moment - java

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.

Related

how to make my AlarmManager work on all phones && How do I get permission to schedule my alarms without ever deleting them

I tried a lot of codes and examples but I didn't find a solution to make my alarms work on all phones because on some phones when the app is destroyed the alarms are completely destroyed, some when I open the app again all the existing alarms start over
Is it true that my app should be in the whitelist? What is the solution?
However, there are apps on the Play Store that work well
#RequiresApi(api = Build.VERSION_CODES.M)
private void setAlarm() {
alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this,AlarmReceiver.class);
pendingIntent = PendingIntent.getBroadcast(this,0,intent,0);
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
Toast.makeText(this,"Alarm set Seccussfely",Toast.LENGTH_SHORT).show();
}
Instead of AlarmManager, need to use WorkManager. AlarmManager is old mechanizm for Android version till 6.0

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

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

Is it possible to set an Android Notification or a later date and time that will fire when the app is not running?

From what I have read it seems that code like this would require the app to be running in a thread until the notification fires. I need the notification to fire at a later date and time so the user sees the notification just like any other notification and then clicks it and it opens of an activity, passing in some data so the app knows what to do.
How can I make this notification fire days later without the app running the whole time?
Do I use wait to accomplish this?
long millis = 60000;
myNotification.wait(millis);
Here is my code which fires immediately
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(R.drawable.star)
.setContentTitle("How was " + me.getString("EventTitle") + "?")
.setContentText("Click here to leave your review");
Intent resultIntent = new Intent(getActivity(), SetupActivity.class);
PendingIntent resultPendingIntent =
PendingIntent.getActivity(
getActivity(),
0,
resultIntent,
PendingIntent.FLAG_UPDATE_CURRENT
);
mBuilder.setContentIntent(resultPendingIntent);
int mNotificationId = me.getInt("EventID");
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getActivity().getSystemService(getActivity().NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, mBuilder.build());
As A--C wrote, use AlarmManager to schedule a PendingIntent to be invoked at your desired time. You will probably use an RTC_WAKEUP alarm, meaning "the time is based on System.currentTimeMillis(), like Calendar uses" and "let's wake up the device out of sleep mode". Use a broadcast PendingIntent, and your Notification code can go into the onReceive() method, if you do not need to do any disk or network I/O to get the information to use for the Notification itself.
Note that in Android 4.4 the rules for AlarmManager changed a bit. You will want to use setExact() on Android 4.4+ and set() on earlier Android versions.

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

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.

Categories