A few years ago, I wrote an alarm app that worked on Android 2, and I'm now trying to upgrade it to work on Android 4. Specifically, on the Samsung Galaxy S4.
On Android 2, if the phone was sleeping, it would wake the phone up and display a "Snooze or Dismiss" screen over the lock screen.
On Android 4, it wakes the phone up, but you have to unlock it, then open the notifications area, then click the alarm's notification, before you can hit "Dismiss."
I have always been using this code to do the waking:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
I have read 8 different stackoverflow questions on this matter. Most of them give the code above, which worked for me years ago in Android 2 but doesn't work in Android 4. But none of them have helped me solve this problem. Here are the questions that I read and tried:
Android: remove or disable programmatically the Lock Screen on Samsung Galaxy S2 device
How to display a fullscreen TYPE_SYSTEM_ALERT window?
How do I create an Activity that is visible on top of the lock screen
How to start a dialog (like alarm dimiss /snooze) that can be clicked without unlocking the screen
Android activity over default lock screen
android device locked, yet want alarm to sound and dialog to appear
Android dialog over lock screen
Show dialog with touch events over lockscreen in Android 2.3
Does anyone have any ideas about what's changed in Android 4 that may have caused this?
EDIT: Here is one of the simplest examples I've seen of an alarm dialog that doesn't come up "minimized." It does not, as written, appear over the lockscreen, but you can fix that with WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
http://wptrafficanalyzer.in/blog/setting-up-alarm-using-alarmmanager-and-waking-up-screen-and-unlocking-keypad-on-alarm-goes-off-in-android/
It's written with a FragmentActivity and a DialogFragment, but it still works as an Activity. It uses an AlertDialog.Builder to make the dialog, and if you try to do it with an XML layout, it won't work. Why?
I figured it out, and the answer was very different from what I expected.
This piece of code was included in the alarm clock sample from Android 2, in the AlarmAlert.java Activity:
#Override
protected void onStop() {
super.onStop();
// Don't hang around.
finish();
}
For reference, you can see the file from the example code in Git's past right here, containing the above onStop function. It never caused a problem in Android 2.
But in Android 4, if the phone was off, this onStop would fire right before the phone woke up, effectively "minimizing" the Activity. Once I removed this function, it immediately worked again.
But I wonder, is this the problem that other people like #radley and #Guardanis are getting? It seems unlikely, but please let me know if this fixes your problems too.
If you're visiting this answer in the future, and you're getting this problem, what I would try is:
Take out any onStop functions.
Add this code to the Activity:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Make sure you're using a full screen theme, and not a dialog theme.
This didn't make a difference for me, but you could try setting showOnLockScreen explicitly in the manifest: <activity android:name="com.example.MyActivity" android:showOnLockScreen="true"/>
A second thing that didn't make a difference for me but you might try is adding the flag WindowManager.LayoutParams.FLAG_FULLSCREEN
I hope this helps other people!
In Kotlin,
For Api level 28 or less, you can simply add below method in your activity that needs to be opened:
override fun onAttachedToWindow() {
super.onAttachedToWindow()
toBeShownOnLockScreen()
}
private fun toBeShownOnLockScreen() {
window.addFlags(
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
or WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
setTurnScreenOn(true)
setShowWhenLocked(true)
} else {
window.addFlags(
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
or WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
)
}
}
And to make it work on Android Pie and above, in additional to above step, we need to set in AndroidManifest as well:
<activity
android:name=".view.activity.LockScreenActivity"
android:showOnLockScreen="true"
android:showWhenLocked="true"
android:turnScreenOn="true" />
I have tested this code from Api level 21 to 29, and works like charm!
Not sure if this is the problem in all cases, but the documentation on ShowWhenLocked says it applies only to the top-most full-screen window. I had a window themed as a dialog which was not working, but it worked fine once I changed it to a regular full-screen window.
One of the questions you linked to has an answer that appeared to solve this issue for me.
This is the code I am using which appears to be working:
#Override
public void onAttachedToWindow() {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onAttachedToWindow();
}
I'm also explicitly declaring this in the activity definition in the manifest:
<activity
android:name="com.example.MyActivity"
android:label="#string/app_name"
android:showOnLockScreen="true"
>
Android activity over default lock screen
Right - So I have been struggling with this one recently but with a 5.0.2 Galaxy Tab A. Unsurprisingly what works on every other device does not work on Samsung (this has been the case since the first Samsung Galaxy device, they break something new each release!)
The general solution for showing an Activity over the lock screen for most devices is
//wake up device and show even when on lock screen
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_FULLSCREEN);
However this does not work for samsung devices. Removing FLAG_DISMISS_KEYGUARD however does this trick.
Looking at the docs for this flag we have
Window flag: when set the window will cause the keyguard to be dismissed, only if it is not a secure lock keyguard. Because such a keyguard is not needed for security, it will never re-appear if the user navigates to another window (in contrast to FLAG_SHOW_WHEN_LOCKED, which will only temporarily hide both secure and non-secure keyguards but ensure they reappear when the user moves to another UI that doesn't hide them). If the keyguard is currently active and is secure (requires an unlock pattern) than the user will still need to confirm it before seeing this window, unless FLAG_SHOW_WHEN_LOCKED has also been set.
and for FLAG_SHOW_WHEN_LOCKED we have
Window flag: special flag to let windows be shown when the screen is
locked. This will let application windows take precedence over key
guard or any other lock screens. Can be used with FLAG_KEEP_SCREEN_ON
to turn screen on and display windows directly before showing the key
guard window. Can be used with FLAG_DISMISS_KEYGUARD to automatically
fully dismisss non-secure keyguards. This flag only applies to the
top-most full-screen window.
You can see they can be used together but it seems samsung will not bother with FLAG_SHOW_WHEN_LOCKED if the device is locked and FLAG_DISMISS_KEYGUARD is present. My app requires a lock screen to be setup so removing the dismiss keyguard flag actually allows me to show full screen Activities over the lock screen. Yay for me, nay for samsung.
Related
I've been trying to use FLAG_SHOW_WHEN_LOCKED and FLAG_DISMISS_KEYGUARD to display an activity each time someone turn the screen on. It works well most of the time but there is some case causing strange behaviour. This happens when the activity is already existing and I send it a intent after it has been moved back in the history stack, by using the Home button.
First, if I press the Home button when on my activity, then switch the screen off and on, the activity is expected to be displayed right away as it was. However what is displayed instead is the basic android lockscreen. My activity is behind.
Worst, sometime, if I touch the home screen, then turn the screen off and on, the home screen overlay (all appli logo and widgets) are displayed with my activity in the background!
The intent I am using each time the screen is turned off to laucn and recall the activity :
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
context.startActivity(intent);
So, it seems that when my activity is in the history stack, it sometimes does not correctly get recalled and bypass the android lockscreen. Is there something wrong with my intent?
Add more flags :
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
Wither via Intent.addFlags() or using getWindow().addFlags() from your activity. These flag will disable lockscreen when your activity is started
This will solve your query
Reference Answer : Here
You can integrate the Adenda SDK in your app to achieve what you're after. They let you display what you want on the lock screen relatively easily.
I'm affiliated with Adenda, for full disclosure
I have set up an AlarmManager to start up an activity. This activity also plays a sound, similar to an alarm app or an incoming call.
It works ok if the screen is on, even if the screen is locked.
If the screen is off, it doesn't work at all. I tried using the following as the first thing in onCreate
getWindow().setFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON, WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
If the screenlock is not enabled, this turns on the screen and I can see my activity closing. I can't hear the sound playing. If the screenlock is enabled, the screen won't turn on at all.
Sometimes I get the following, but not always:
07-18 23:52:13.685: E/OpenGLRenderer(14148): GL_INVALID_OPERATION
How can I make it start properly when the screen is off?
I got my answer partially from here.
lock = ((KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE)).newKeyguardLock(KEYGUARD_SERVICE);
powerManager = ((PowerManager) getSystemService(Context.POWER_SERVICE));
wake = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
lock.disableKeyguard();
wake.acquire();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
A while back I read that your app must be in full screen for the FLAG_TURN_SCREEN_ON to work.
"** One important note. Your activity must be full screen in order for the above flag combination to work. In my app I tried to use these flags with an activity which is not full screen (Dialog Theme) and it didn't work. After looking at the documentation I found that these flags require the window to be a full screen window." -Wake Android Device up
Quote from someone who posted their about a similar issue with FLAG_X.
Look into running a service, activity is going to be stopped when not in foreground.
Also look into the Activity lifecycle. http://developer.android.com/reference/android/app/Activity.html
I'm trying to start an activity from a service I had already acquired the lock for as follows:
Intent i = new Intent(context, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(i);
The activity manifest is declared as follows:
<activity
android:name=".MyActivity"
android:configChanges="orientation|screenSize|keyboardHidden|keyboard|navigation"
android:excludeFromRecents="true"
android:launchMode="singleInstance"
android:screenOrientation="nosensor"
android:showOnLockScreen="true"
android:taskAffinity=""
android:theme="#style/MyTheme" />
And finally, on onCreate() or on onAttachedToWindow() (I tried on both), I add the following flags:
final Window win = getWindow();
win.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
win.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
The problem is that the first time I call startActivity() from my service, the screen turns on but the activity itself does not show up. It shows the lock screen instead. Every subsequent call of startActivity() works properly but I can't find a reason for this odd behavior.
I tried already suggestions to get a full wakelock instead of partial, change the flags and values in the manifest according to the following SO answers:
Android Activity Not Showing When Screen is Woken Up and Lock Screen Not Disabling
how to unlock the screen when BroadcastReceiver is called?
Programmatically turn screen on in android
Android Galaxy S4 -- Activity that is visible over lock screen
Note that my theme is not a dialog but a fullscreen activity.
Any other ideas?
I'm facing the same problem, after a lot of searching here and google, found this which unlocked the screen and popped my activity but it only works for me when the app is running (foreground/background).
import android.view.Window;
import android.view.WindowManager.LayoutParams;
Window window = this.getWindow();
window.addFlags(LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED);
window.addFlags(LayoutParams.FLAG_TURN_SCREEN_ON);
i'm trying to start an activty when app is closed... (using broadcast receiver)
in the docs (for example here) and most of the answers on SO the flags are added this way:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
but when i tried the way it is like in the example it unlocked the screen instead of just turning on the screen.
hope this help . it still didn't solve my problem completely.
EDIT:
found this post which solved my problem.
there is a comment there on NOT using a dialog theme which solved it for me
Step 1: Add below code in your activity before
setContentView(R.layout.activity_about_us);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD|
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
Step 2: Lock your mobile than you will see activity in which you have added this code.
You can implement this if you want to open particular screen by notification occurrence like skype call.
Since my application already includes a Service, this is what I do: if the screen is locked, I register a broadcast receiver (a bit simpler than this one, since it reacts only on unlocking) that starts the Activity as soon as the screen gets unlocked.
I want to build a dialog which is visible on the lockscreen and can receive touch events. I built a window with WindowManager but only the TYPE_SYSTEM_OVERLAY Flag is shown over the lockscreen in GB (Android 2.3.7).
Is there a way to create a system overlay which is visible on the lockscreen and can receive touch events in Android 2.3.7?
There was a bug with FLAG_WATCH_OUTSIDE_TOUCH but I'm not sure how that affects me. Any ideas?
I do not think you can launch an activity each time when device is locked without binding your application as admin privilaged app programatically.
Once your app is admin privilaged app, you can programatically set password & lock the screen & then programatically unlock it using Device Policy Manager.
On top of that lock screen you can programatically launch your own activity & you can create your own unlocker & unlock device through that activity as you can get call backs via DeviceAdminReceiver.
Here is a good example for that & all you need is to create your own activity after you called DevicePolicyManager.lockNow(). Then it will appear on top of lock screen as normal activity plus extra control over native lockscreen.
Try this It may helps you,
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.alertdialog);
And also, Android is a little bit of a contradiction. It's very open and as a developer you have access to anything, and it's up to you to use those powers for good or evil. When I say evil I don't mean malware. I mean apps that try to get cute and use things in ways they weren't meant to be used, like putting up notifications asking you to use the app more. The contradiction is that you don't actually have access to everything, there are a few parts the developers decided were so important that app couldn't mess with them. The lock screen is one of those parts. You can replace your home app all you want, but you never have to worry about your replacement lock screen failing and preventing you from accessing your phone.
Even if this were possible you would have more problems to deal with. Every lock screen is different, manufacturers can and do customize it so you have no guarantees your activity won't get in the way of unlocking the phone.
For touching outside of your dialog,
dialog.setCanceledOnTouchOutside(your boolean);
Finally I achieved the same. Don't go for activity, because android will not show lock screen behind your activity for security reason, so for service.
Below is my code in onStartCommand of my service
WindowManager mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
View mView = mInflater.inflate(R.layout.score, null);
WindowManager.LayoutParams mLayoutParams = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, 0, 0,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
/* | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON */,
PixelFormat.RGBA_8888);
mWindowManager.addView(mView, mLayoutParams);
The Context
Recently, I have been looking for reliable ways to control a secured Android Keyguard. Mainly to display a custom lock screen. I know that Google had stated custom lock screens are not officially supported by the platform and should expect things to break, however, with the existing APIs, I believe there must be ways to do this. I have done tons of research for about a week but still having problem here and there. What I have implemented, assuming a secured Keyguard is enabled, so far are,
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a window) to be displayed on screen on, putting the Keyguard behind, and all unsafe actions are prevented. Notification panel is disabled, finishing the activity will bring up the Keyguard. I implemented as following in my lock screen activity.
#Override
public void onAttachedToWindow() {
window.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
}
KeyguardManager, KeyguardManager.KeyguardLock are deprecated classes, but they still work all the way to Jelly Bean. To do this, I have a Service that handles two things, holding a static KeyguardManager and the related objects, and have it hold a BroadcastReceiver to receive Intent.ACTION_SCREEN_ON and Intent.ACTION_SCREEN_OFF. (all the objects are initialized properly)
For ScreenReceiver
public static synchronized void disableKeyguard() {
if ( isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.disableKeyguard();
isLocked = false;
}
}
public static synchronized void reenableKeyguard() {
if ( !isLocked ) {
if ( keyguardLock == null ) {
keyguardLock = keyguardManager.newKeyguardLock(LOG_TAG);
}
keyguardLock.reenableKeyguard();
keyguardLock = null;
isLocked = true;
}
}
For BroadcastReceiver
#Override
public void onReceive( Context context, Intent intent ) {
if ( intent.getAction().equals(Intent.ACTION_SCREEN_ON) ) {
Intent start = new Intent(context, LockScreen.class);
start.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
context.startActivity(start);
} else if ( intent.getAction().equals(Intent.ACTION_SCREEN_OFF) ) {
ScreenReceiverService.reenableKeyguard();
}
}
For LockScreenActivity, when the user had input the correct passcode,
window.clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
ScreenReceiverService.disableKeyguard();
finish();
The Problem
Things that works
ACTION_ON and ACTION_OFF are received reliably.
LockScreenActivity is shown before the Keyguard (without telephone state handling yet)
Notification cannot be pulled down, exiting the activity in any way would display the lockscreen.
Things that does not work
After I disable Keyguard and call finish(), my app exits and homescreen or the last activity before the screen went off is shown. However, whenever I press the Home Key, the Keyguard will flash into the screen, quickly dismissing itself immediately, and the normal Home Key function/event is not handled (will not return to homescreen after flashing). This is observed when I rapidly tapped the Home Key repeatedly.
I even looked into the Android source code to find out the Home Key handling, but it is never sent to third-party applications unless the window type is WindowManager.LayoutParams.TYPE_KEYGUARD or WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG, which will throw SecurityException on 4.0+ even it worked on earlier platforms. And for the Keyguard, I have declared DISABLE_KEYGUARD permission use this shouldn't be the problem. My guess is the flag FLAG_SHOW_WHEN_LOCKED will tell the system to handle to Keyguard in some ways that would conflict with other disable calls. Since this flag is mostly used for Alarm/SMS type application, which is to show limited information to the user, then dismiss themselves and bring up the Keyguard. But in my case, having the user unlock my lock screen then unlock the system lockscreen simply defeats the purpose of my app.
So the question is why would the Keyguard flashes whenever I press Home after I disabled it? Is there any workaround/solution for this issue?
P.S. Thank you for reading such a long question. This is my first time asking a question here, if there is anything that I did wrong, please tell me (i.e. format, grammar, code convention, tags, etc.). Also I had no experience with any programming knowledge, I started with Android before I know what Java is. So I have not taken any proper course/training yet, this community is awesome and often help people like I even if they are simple questions, and of course watching Google I/O videos, reading blogs, read others' code help me a lot. So please tolerate any dumb mistakes/obvious bugs/stupid questions. I am only 16. ^_^"
I have used this with some success in both Gingerbread and ICS to open my activity (via a background service which is starting it). In the activity being started:
#Override
public void onAttachedToWindow() {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
}
I had the same problem for the click of HOME button while unlocking the device. This can be solved by reseting the password to blank (ie "") :
DevicePolicyManager devicePolicyManager;
ComponentName demoDeviceAdmin;
devicePolicyManager.setPasswordQuality(demoDeviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
devicePolicyManager.setPasswordMinimumLength(demoDeviceAdmin, 0);
devicePolicyManager.resetPassword("",DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
and then disabling the keygaurd :
this.keyGuardLock = ((KeyguardManager)getSystemService("keyguard")).newKeyguardLock("keyguard");
keyGuardLock.disableKeyguard();
Hope this solved your problem. \m/ keep coding!
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED allows an activity(a
window) to be displayed on screen on, putting the Keyguard behind
I tried to get this but my activity always preceded by the system lock screen. isOrderdBroadcast() says that ACTION_SCREEN_NO is an ordered broadcast.
I added flag to the activity :
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
in onAttachedView(). But still the system lock is getting the preference over my Custom screen lock activity.
How did you get your activity before the system lock screen?
EDIT
On a hindsight, I think my understanding of the lock screen concept was wrong. My broadcast receiver was getting the broadcast first. But what was showing before that was the system lock screen launched when SCREEN_OFF is received. Fixed that problem as of now.
But stumped by the ambiguity of home button behavior. This won't be a problem in post ICS devices as all hard buttons are discouraged.
In your LockScreenActivity, ending the validation code by finish(); kills the LockscreenActivity and thus the whole app. Instead of that, you could just launch back your main activity (or any other) like this :
startActivity(new Intent(LockScreenActivity.this, MainActivity.class));
If AOSP is in your control then you need to set the simple flag and keyguard() is gone for good.
Here is the details to do that, get into the file
"overlay/frameworks/base/packages/SystemUI/res/values/config.xml"
and search for "config_enableKeyguardService" then set the flag to false.
NO MORE keyGuard, pheww