Before anyone reports my question as duplicate, I've already checked dozens of similar questions but had no success whatsoever.
I've made an alarm clock app that seems to work on all devices but mine, it's either that or none of the users have bothered reporting the issue to me.
The problem I'm having is my alarm is not triggering when my device is asleep. I have often woken up by myself a few minutes after the alarm was supposed to go off and as soon as I simply press the power button, my phone wakes up and then the alarm goes off.
My app works absolutely fine if I'm using my phone though, which is not really the ideal purpose it's been designed for.
If I kill the process it won't go off either.
Below is my code, hope you guys can help me:
public static void scheduleAlarm(Context context, Alarm alarm)
{
AlarmRepository.getInstance(context).update(context, alarm.getId(), alarm);
AlarmManager manager = (AlarmManager)context
.getSystemService(Context.ALARM_SERVICE);
long triggerTime = BackEndTools
.getNextValidTriggerTime(alarm);
/* getNextValidTriggerTime calculates whether the alarm
* should go off on the following day, in case it's been
* set to a time equal to or before now */
Intent intent = new Intent(context, AlarmReceiver.class);
intent.setAction(AppConstants.ACTION_TRIGGER_ALARM);
intent.putExtra(AppConstants.EXTRA_ID, alarm.getId());
PendingIntent pendingIntent =
PendingIntent.getBroadcast(context, alarm.getId(), intent, 0);
if (AppConstants.OS_VERSION < Build.VERSION_CODES.KITKAT)
manager.set(RTC_WAKEUP, triggerTime, pendingIntent);
else
manager.setExact(RTC_WAKEUP, triggerTime, pendingIntent);
}
Now here's my BroadcastReceiver
:
public class AlarmReceiver extends WakefulBroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
int id = intent.getIntExtra(AppConstants.EXTRA_ID, AppConstants.DEFAULT_INTENT_EXTRA);
Alarm alarm = AlarmRepository.getInstance(context).select(context, id);
if (intent.getAction().equals(AppConstants.ACTION_TRIGGER_ALARM))
AlarmHandler.handleReceivedAlarm(context, alarm);
else if (intent.getAction().equals(AppConstants.ACTION_SLEEP_CHECKER))
AlarmHandler.handleReceivedSleepChecker(context, alarm);
else if (intent.getAction().equals(AppConstants.ACTION_SNOOZE))
AlarmHandler.handleSnoozeTask(context, alarm);
}
}
This is AlarmHandler.handleReceivedAlarm
:
public static void handleReceivedAlarm(Context context, Alarm alarm)
{
Intent activityIntent = new Intent(context, AlarmActivity.class);
activityIntent.putExtra(AppConstants.EXTRA_ID, alarm.getId());
activityIntent.setAction(AppConstants.ACTION_TRIGGER_ALARM);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(activityIntent);
}
Finally, the relevant parts of my AndroidManifest
:
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<receiver
android:name=".backend.AlarmReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="com.mypackage.myapp.ACTION_TRIGGER_ALARM"/>
<action android:name="com.mypackage.myapp.ACTION_SLEEP_CHECKER"/>
<action android:name="com.mypackage.myapp.ACTION_SNOOZE"/>
</intent-filter>
</receiver>