AlarmManager.setExact() is always slow - android

The alarms in my app are consistently around 40-50 seconds late.
I'm assuming this is due to a problem in the AlarmManager.setExact() but I can't be sure.
Here is my schedule function calling setExact():
fun schedule(context: Context) {
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(context, AlarmReceiver::class.java)
intent.putExtra("recurring", recurring)
val alarmPendingIntent = PendingIntent.getBroadcast(
context,
alarmId, intent, if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT
} else {
PendingIntent.FLAG_UPDATE_CURRENT
}
)
val calendar: Calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, hour)
calendar.set(Calendar.MINUTE, minute)
//if alarm time has already passed, increment day by 1
if (calendar.timeInMillis <= System.currentTimeMillis()) {
calendar.set(Calendar.DAY_OF_MONTH, calendar.get(Calendar.DAY_OF_MONTH) + 1)
}
val toastTime = if (minute < 10) {
"$hour:0$minute"
} else {
"$hour:$minute"
}
if (!recurring) {
Toast.makeText(
context,
"One Time Alarm set for $toastTime",
Toast.LENGTH_LONG
).show()
alarmManager.setExact(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
alarmPendingIntent
)
} else {
Toast.makeText(context, "Alarm set daily for $toastTime", Toast.LENGTH_LONG)
.show()
val runDaily = (24 * 60 * 60 * 1000).toLong()
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
calendar.timeInMillis,
runDaily,
alarmPendingIntent
)
}
started = true
}
If there is no solution available, is there maybe a better alternative to the AlarmManager?

Related

AlarmManager set is not firing broadcast

I'm trying to schedule notifications using the AlarmManager but for whatever reason, the onReceive method on my receiver isn't firing.
Here's how I'm scheduling the alarm
val intent = Intent(this, ReminderReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val cal: Calendar = Calendar.getInstance()
cal.add(Calendar.SECOND, 5)
val amanager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
amanager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
and here's the onReceive method
override fun onReceive(context: Context, intent: Intent) {
Toast.makeText(context,"notification", Toast.LENGTH_SHORT).show()
}
I tried manually sending a broadcast to see if it works and there's no problem there so the issue shouldn't be related to the manifest.
I'm running this on MIUI12 android 10
I see no issue with your logic if your problem still prevails, try the following as a workaround
val intent = Intent(this, ReminderReceiver::class.java)
val pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT)
// val cal: Calendar = Calendar.getInstance()
// cal.add(Calendar.SECOND, 5)
val triggerTime = SystemClock.elapsedRealtime() + 5_000 // five seconds from now
val amanager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
// amanager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
AlarmManagerCompat.setExactAndAllowWhileIdle(
amanager,
AlarmManager.ELAPSED_REALTIME_WAKEUP,
triggerTime,
pendingIntent
)

Can't cancel alarm with AlarmManager.cancel() on 24 API

Have 2 methods:
addAlarm()
val myIntent = Intent(context, AlarmReceiver::class.java)
if (SDK_INT > Build.VERSION_CODES.M) {
myIntent.action = "ADD_ALARM"
} else {
myIntent.putExtra("alarm", alarm)
}
val pendingIntent = PendingIntent.getBroadcast(context, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
if (SDK_INT < Build.VERSION_CODES.M)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
else
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
/*
if I try to cancel alarm after creating like this:
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
alarmManager.cancel(pendingIntent)
it would work and alarm would be canceled. Seems like problem is
in pendingIntent.
*/
deleteAlarm()
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
var myIntent = Intent(context, AlarmReceiver::class.java)
//with flag FLAG_NO_CREATE return null
val pendingIntent = PendingIntent.getBroadcast(context, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.cancel(pendingIntent)
Everything works great on api 23, but on api 24 alarm do not canceling. Is it problem with intent.action? What did i miss?
Ok, I solved it with adding intent.action like this:
val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
var myIntent = Intent(context, AlarmReceiver::class.java)
if (SDK_INT > Build.VERSION_CODES.M) {
myIntent.action = "ADD_ALARM"
}
val pendingIntent = PendingIntent.getBroadcast(context, 1, myIntent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.cancel(pendingIntent)

How to set multiple alarms in kotlin?

so I'm new to coding and I'm making an app for Muslims prayer times and I want an alarms to be set repeatedly on these times
I tried to many things but It doesn't works!
I have a praytimes class and if anyone wants it I'll put it
I will be more thankful, if you could help me on how to set an alarm for 1 prayer so I could do the rest.
Main Activity
val latitude = 30.354802
val longitude = 42.2461069
val timezonoffset = 3.0
val timeZoneId = "Middle East/Alowiqila"
val prayTimes = PrayTimes()
prayTimes.timeFormat = prayTimes.time12//
prayTimes.calcMethod = prayTimes.makkah// Muslim World League (MWL)
prayTimes.asrJuristic = prayTimes.hanafi// Shafii (standard)
prayTimes.adjustHighLats = prayTimes.angleBased
val offsets = intArrayOf(0, 0, 0, 0, 0, 0, 0) // {Fajr,Sunrise,Dhuhr,Asr,Sunset,Maghrib,Isha}
prayTimes.tune(offsets)
val cal = Calendar.getInstance(TimeZone.getTimeZone(timeZoneId))
cal.time = Date()
val times = prayTimes.getPrayerTimes(cal, latitude, longitude, timezonoffset)
println("prayer times for Alowiqila")
System.out.println("Fajr : " + times.get(0))
System.out.println("Sunrise : " + times.get(1))
System.out.println("Duhr : " + times.get(2))
System.out.println("Asr : " + times.get(3))
System.out.println("Sunset : " + times.get(4))
System.out.println("Magrib : " + times.get(5))
System.out.println("Isha : " + times.get(6))
// I want the Alarm to be set to the code above it
fun startAlarm(isNotification:Boolean, isRepeat:Boolean) {
val manager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val myIntent= Intent(this#MainActivity, AlarmReceiver::class.java)
val pendingIntent:PendingIntent
// SET TIME HERE
val calendar = Calendar.getInstance()
calendar.set(Calendar.HOUR_OF_DAY, 15)
calendar.set(Calendar.MINUTE, 20)
pendingIntent = PendingIntent.getBroadcast(this, 0, myIntent, 0)
if (!isRepeat)
manager.set(AlarmManager.RTC_WAKEUP, SystemClock.elapsedRealtime() + 3000, pendingIntent)
else
manager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, AlarmManager.INTERVAL_DAY, pendingIntent)
}
AlarmReceiver.kt
and here is my alarm receiver in witch I've added me notifications
class AlarmReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val builder = NotificationCompat.Builder(context)
val myIntent = Intent(context, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(
context,
0,
myIntent,
FLAG_ONE_SHOT
)
builder.setAutoCancel(true)
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(System.currentTimeMillis())
.setContentTitle("موعد الاذان")
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_LIGHTS or Notification.DEFAULT_SOUND)
.setContentInfo("Info")
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, builder.build())
}
You have to put different requestCode in pending intent, each alarm manager requires different requestCode.
in your mainActivity
pendingIntent = PendingIntent.getBroadcast(this, (0..2147483647).random(), myIntent, 0)

AlarmManager setEexact() not working

I have have two AlarmManager scheduled to fire once a day at 23:59 and another one that fires at the same time but once a week.
Below is the two functions that do this:
private fun midnightTimeClearCache(dataType: DataType, requestCode: Int) {
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
val intent = Intent(context, ClearDataReceiver::class.java)
intent.putExtra(ClearDataReceiver.DATA_TYPE_EXTRA, dataType.name)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
}
private fun weekTimeClearCache(dataType: DataType, requestCode: Int) {
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
var i: Int = calendar.get(Calendar.WEEK_OF_MONTH)
calendar.set(Calendar.WEEK_OF_MONTH, i++)
val intent = Intent(context, ClearDataReceiver::class.java)
intent.putExtra(ClearDataReceiver.DATA_TYPE_EXTRA, dataType.name)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
}
Below is the reciever that should get fired as part of the above pending Intents
class ClearDataReceiver : DaggerBroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
super.onReceive(context, intent)
var bundle = intent!!.extras
val dataTypeString = bundle.getString(DATA_TYPE_EXTRA)
val dataType = DataType.valueOf(dataTypeString)
Log.d("JJJ", "clearing data for " + dataTypeString)
when (dataType) {
DataType.CUSTOMER_DETAILS -> {
storage.clearDetails()
storageClearSchedular.setCustomerDetailsClear()
}
DataType.CUSTOMER_PIC -> {
storage.clearPic()
storageClearSchedular.setPicClear()
}
}
here is my manifest:
And the boot reciever so i can reset the alerm manager
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("JJJ", "onReceive")
storageClearSchedular = StorageClearSchedular(context!!, context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager)
storageClearSchedular.setAllSchedulars()
When i set the above alert manager schedules and then kill the app from the background before i head to bed, i then load up the app again to find that it diddnt execute my broadcast reciever(no details on the logs and data not cleared).
What should happen is that it should execute every dat at the time i set or once a week and then set it again after it has been fired but it doesnt seem to work at all.
I first tried setRepeating but that diddnt work as im targeting devices from api 19 and above
What am i missing?
For api version above 19 use this code
if (Build.VERSION.SDK_INT < 19) {
alarmManager.set(AlarmManager.RTC,
calendar.getTimeInMillis(), pendingIntent);
}
} else if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT <= 22) {
alarmManager.setExact(AlarmManager.RTC,
calendar.timeInMillis, pendingIntent)
} else if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC,
calendar.timeInMillis, pendingIntent)
}
For the daily alarm, you can check the condition. This example will set alarm for 9 AM Daily. Also, you may need to set the alarm again when the device restarts or boots up because all the alarms get canceled once the device is powered off or restarted.
Calendar calendar = Calendar.getInstance()
// set selected time from timepicker to calendar
calendar.setTimeInMillis(System.currentTimeMillis());
// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
Log.e("", "Alarm will schedule for next day!");
calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
else{
Log.e("", "Alarm will schedule for today!");
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);

Android Alarm Not Set

I used to set alarms using the following code-segment in other projects as repeating and non-repeating, but it's now driving me crazy about what may the silly mistake be that I've made for not the alarm speaking to my current implementation :\ :
private fun setAlarm(obj: MyObject, time: Long) {
val intent = Intent(applicationContext, MyAlarmIntentService::class.java)
intent.putExtra(C.KEY_ME, obj)
val pendingIntent = PendingIntent.getService(applicationContext, 43, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
val context = this#MyActivity
val cal = Calendar.getInstance()
cal.timeInMillis = time * 1000 // time is in seconds
Log.d("setAlarm", "setting time -> $time for obj = $obj")
Log.d("setAlarm", "Set calendar: " + cal.toString())
val alarmManager = getSystemService(Context.ALARM_SERVICE) as AlarmManager
#RequiresApi(Build.VERSION_CODES.LOLLIPOP) // Overriding doze-mode
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
val alarmClockInfo =
AlarmManager.AlarmClockInfo(cal.timeInMillis, null)
alarmManager.setAlarmClock(alarmClockInfo, pendingIntent)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) // Overriding doze-mode
// Not-working in Nexus 6 - API 25 (7.1.1), but works in API 23 (6.0.1, tested in multiple devices)
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP,
cal.timeInMillis, pendingIntent)
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Works
alarmManager.setExact(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
else // works
alarmManager.set(AlarmManager.RTC_WAKEUP, cal.timeInMillis, pendingIntent)
}
The IntentService class is as the following:
class MyAlarmIntentService : IntentService("Notification") {
override fun onHandleIntent(intent: Intent?) {
val obj = intent?.getParcelableExtra<MyObj>(C.KEY_ME) ?: return
Lg.d(TAG, "My alarm fired for - \n" + obj.toString())
}
companion object {
private val TAG = ContestAlarmIntentService::class.java.simpleName
}
}
The service is declared in manifest as:
<service android:name=".services.MyAlarmIntentService" />
The following is an example of the mentioned log inside setAlarm() method:
E/setAlarm: setting time -> 1514993850 for obj = MyObj{...}
E/setAlarm: Set calendar: java.util.GregorianCalendar[time=1514993850000,areFieldsSet=true,areAllFieldsSet=true,lenient=true,zone=libcore.util.ZoneInfo[id="Asia/Dhaka",mRawOffset=21600000,mEarliestRawOffset=23400000,mUseDst=false,mDstSavings=0,transitions=7],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=0,WEEK_OF_YEAR=1,WEEK_OF_MONTH=1,DAY_OF_MONTH=3,DAY_OF_YEAR=3,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=1,AM_PM=1,HOUR=9,HOUR_OF_DAY=21,MINUTE=37,SECOND=30,MILLISECOND=0,ZONE_OFFSET=21600000,DST_OFFSET=0]
This used to be a straight-forward code to me, but which fundamental may I be missing for these 3 days' of test sessions?
Sorry I did not go through all your code as I was busy in another task, so here's my code used with Broadcast
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction(UtilAlarmConstants.ALARM_ACTION);
pIBroadCast = PendingIntent.getBroadcast(context, ALARM_PI_REQ_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
alarmManager = (AlarmManager) context.getApplicationContext().getSystemService(Context.ALARM_SERVICE);
if (alarmManager != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
alarmManager.setExactAndAllowWhileIdle(ALARM_TYPE, calendar.getTimeInMillis(),pendingIntent);
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
AlarmManager.AlarmClockInfo alarmClockInfo
= new AlarmManager.AlarmClockInfo(System.currentTimeMillis() + fireAT, null);
alarmManager.setAlarmClock(alarmClockInfo, pIBroadCast);
}
//setExact for 19
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, fireAT, pIBroadCast);
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, fireAT, pIBroadCast);
}
}
and Broadcast
if (intent != null && intent.getAction() != null) {
if (intent.getAction().equalsIgnoreCase(UtilAlarmConstants.ALARM_ACTION)) {
setUpNotification(context, intent);}
sorry for not giving answer related to service as they are all same wacky dacky :)

Categories

Resources