2 years ago
#29971
sammy
PendingIntent.FLAG_NO_CREATE returns null after restarting emulator a couple of times
I am trying to check if the alarm I set exists by passing in the same request code and intent values I have used while setting the alarm. Now, I have set the alarm to repeat every minute. It works as it should but after a couple of restarts, it stops working. I have used the following code to check if the alarm of interest exists:
val alarmExists = getPendingIntent(
id,
intent,
PendingIntent.FLAG_NO_CREATE) != null
From what I understand, this should return true if the alarm exists. Now, when I run my emulator for the first few times, the above code returns true. However, the funny part is when I re-run my emulator for about 3-4 times, that's when it starts to return false. I am not sure what is causing this to happen and don't know what to try out. Why is the system suddenly not able to find the intent after returning true for the first few runs?
Here's the code I've used:
AlarmService.kt
private val alarmManager: AlarmManager =
context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
fun setRepeatingAlarm {
alarmManager.setRepeating(
AlarmManager.RTC_WAKEUP,
alarmTimeMillis,
60000L,//trigger alarm every 60 seconds after set time
getPendingIntent(intent, id, PendingIntent.FLAG_UPDATE_CURRENT)
)
}
private fun getIntent(action: String): Intent {
val intent = Intent(context, AlarmReceiver::class.java)
intent.action = action
return intent
}
private fun getPendingIntent(intent: Intent, id: Int, flag: Int): PendingIntent {
return PendingIntent.getBroadcast(
context,
id,
intent,
flag)
}
AlarmUtils.kt
fun checkActiveAlarm() {
//fetch alarm ids stored in shared pref
val idSet = alarmPref.getIds()
val intent = getIntent(Keys.MY_ALARM.action)
val iter = idSet.iterator()
while (iter.hasNext()) {
val id = iter.next()
val alarmExists = getPendingIntent(
id,
intent,
PendingIntent.FLAG_NO_CREATE) != null
if (alarmExists) println("Alarm $id exists")
else println("Alarm not set")
}
}
private fun getIntent(action: String): Intent {
val intent = Intent(context, AlarmReceiver::class.java)
intent.action = action
return intent
}
private fun getPendingIntent(id: Int, intent: Intent, flag: Int): PendingIntent? {
return PendingIntent.getBroadcast(context, id, intent, flag)
}
AndroidManifest.xml
<receiver
android:name=".notificationScreens.AlarmReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Here are the permissions I have set:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
java
android-studio
kotlin
android-intent
alarmmanager
0 Answers
Your Answer