As the title says my broadcast receiver is never called.
I'm at my wits end, I know there are 30.. thousand posts about this, and I swear I have read through most of them and tried a dozen things but I am either missing something obvious or I have a weird problem.
Nothing is crashing, there are no errors or warnings.
Here are the relevant portions of my Manifest:
<receiver android:name=".AlertReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name=".AlertReceiver"/>
</intent-filter>
</receiver>
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
Here is the creation methods in my MainActivity:
private void startAlarm(Calendar c) {
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, AlertReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 1, intent, FLAG_IMMUTABLE);
if (c.before(Calendar.getInstance())) {
c.add(Calendar.DATE, 1);
}
syncAlarm(this, c.getTimeInMillis(), alarmManager, pendingIntent);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
Log.d("MainAcitivy", "Created Alarm")
}
private void syncAlarm(Context context, long time, AlarmManager am, PendingIntent pending) {
Intent intent = new Intent(this, AlertReceiver.class);
if (Build.VERSION.SDK_INT >= 23) {
// Wakes up the device in Doze Mode
am.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, time, // time in millis
pending);
} else if (Build.VERSION.SDK_INT >= 19) {
// Wakes up the device in Idle Mode
am.setExact(AlarmManager.RTC_WAKEUP, time, pending);
} else {
// Old APIs
am.set(AlarmManager.RTC_WAKEUP, time, pending);
}
}
And here is my Broadcast Receiver:
public class AlertReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"AlertReceiver called", Toast.LENGTH_LONG).show();
Log.d("Broadcast Receiver", "Entered onReceive");
}
}
Thank you for your time.
EDIT: Okay so turns out it is technically working, but it only seems to trigger when i do a dance I havent quite figured out where I lock and unlock my screen after it has passed the time when the alarm should have triggered. It doesnt trigger if I leave the app open. I think I must be missing a setting.
Well I found the problem:
I played around with some exact alarms and it worked well, just had to dig a bit deeper into the documentation to find that what was on the surface was a lie.