Search code examples
androidandroid-intentbroadcastreceiveralarmmanagerandroid-alarms

Android Alarm not working in Release


I'm new to android and this is my first app.

I created my app and everything works fine the alarm triggers. After I create a signed release and install my alarm does not work :(.

This is my Alarm, to register the receiver :

public void enable(){
    if(enabled == true) return;
    IntentFilter filter = new IntentFilter();
    filter.addAction(INTENT);
    mContext.registerReceiver(this, filter);
    enabled = true;
}

Then to set the alarm:

private void setAlarm(Long interval){
    Intent startIntent = new Intent(INTENT);
    mPendingIntent = PendingIntent.getBroadcast(context, 0, startIntent, 0);
    AlarmManager alarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    alarm.setInexactRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 60000, 60000, mPendingIntent);
}

Initially I call enable, then setAlarm.

It's all working fine in non release. I have not put the receiver in my manifest because I'm registering manually. The set alarm method is being called, I know this for certain as I made a toast to test it. However it never receives any broastcasts.


Solution

  • Okay, I found the problem.

    Since this is a live wallpaper, the engine is invoked twice, once for preview, once for the actual wallpaper once you've set it.

    The order of events dictates whether the alarm would work. It turns out that if the preview is destroyed after the paper is created then all of my alarms are turned off, because the intents match??

    AlarmManager alarm = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
    alarm.cancel(mPendingIntent);
    

    So what I have done is not register alarms on preview - why would you need them here anyway. Although my new question is what would be the best way to get around this if I couldn't disable for preview. A unique intent for each paper?

    Interestingly the reason why this was only picked up on release was because its rare during testing I would go through the manual set wallaper process, so it was left not picked up till now.