I had the same problem once and I created a Service
that was running in the background. It had a TimerTask
loop that ran every second and checked wether my LockScreenActivity
was in the foreground.
You can do that like this:
List<ActivityManager.RunningTaskInfo> runningTasks = activityManager.getRunningTasks(4096);
return runningTasks.get(0).topActivity.getClassName();
If my lockscreen wasn't in the foreground, I started it up/ brought it to the foreground again (make it singleTop in the Manifest).
TronicZomB's answer looks more sophisticated though.