After re-thinking how I was doing this, I opted to handle all of the wakelock and keyguard code in the Service, which seems to work pretty well. So AlarmActivity just starts the Service (I removed all the other code from it), and ScreenOffReceiver stayed the same. Here is the updated Service code in case it helps anyone in the future:
public class KeyguardService extends Service {
BroadcastReceiver mReceiver = null;
Boolean keyguardDisabled;
PowerManager pm;
WakeLock myWakeLock;
KeyguardManager km;
@SuppressWarnings("deprecation")
KeyguardManager.KeyguardLock keylock;
@SuppressWarnings("deprecation")
@Override
public void onCreate() {
super.onCreate();
// code to turn screen on
// acquire wakelock
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
myWakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "MyWakelock");
// disable keyguard
km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keylock = km.newKeyguardLock("myLock");
keylock.disableKeyguard();
// ensure secure keyguard won't be bypassed
km.exitKeyguardSecurely(null);
// acquire wakelock that will timeout based on current user setting
myWakeLock.acquire(Settings.System.getLong(getContentResolver(), Settings.System.SCREEN_OFF_TIMEOUT, 1000));
keyguardDisabled = true;
// REGISTER RECEIVER THAT HANDLES SCREEN OFF LOGIC
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
mReceiver = new ScreenOffReceiver();
registerReceiver(mReceiver, filter);
}
@SuppressWarnings("deprecation")
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (keyguardDisabled == true && ScreenOffReceiver.screenTurnedOff == true) {
keylock.reenableKeyguard();
keylock = null;
if (myWakeLock.isHeld()) {
myWakeLock.release();
myWakeLock = null;
}
keyguardDisabled = false;
ScreenOffReceiver.screenTurnedOff = false;
stopSelf();
}
return 1;
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onDestroy() {
if(mReceiver!=null) {
unregisterReceiver(mReceiver);
}
}
}