2

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>
92AlanC
  • 1,327
  • 2
  • 14
  • 33
  • 1
    http://stackoverflow.com/a/33110418/2444099 – Eugen Pechanec Dec 24 '16 at 02:09
  • @EugenPechanec please post it as an answer, it worked! – 92AlanC Dec 24 '16 at 15:47
  • @EugenPechanec it's not a duplicate question. Although it worked on my first test, when I tested it again, but killing the process before the alarm was supposed to go off it didn't work. – 92AlanC Dec 24 '16 at 16:14
  • When you kill the process any information stored in memory is lost. My guess is `AlarmRepository` has a naive implementation (it's not persisted to hard drive) and is lost on process death. – Eugen Pechanec Dec 24 '16 at 16:24
  • Not really, `AlarmRepository` is an SQLite database, I can post the code if you need – 92AlanC Dec 24 '16 at 16:36
  • How exactly did you kill the process? http://stackoverflow.com/a/11658508/2444099 Offtopic: Extending WakefulBroadcastReceiver alone does nothing in terms of wake lock. Read [docs](https://developer.android.com/reference/android/support/v4/content/WakefulBroadcastReceiver.html). – Eugen Pechanec Dec 24 '16 at 16:40
  • I basically cleared the app from the recents list, not sure if it actually kills the process though – 92AlanC Dec 24 '16 at 16:42
  • *Before anyone reports my question as duplicate, I've already checked dozens of similar questions but had no success whatsoever.* that is a very ignorant pre-emptive and not conducive to getting the help. – t0mm13b Dec 24 '16 at 16:49

0 Answers0