Using disableKeyguard and reenableKeyguard to lock and unlock - android

In my application I am using disableKeyguard and reenableKeyguard to lock and unlock a screen with proximity and accelerometer sensors respectively. My application working with some bugs. If user unlocks a screen by dragging (without using my app) then my application will not lock !. How to prevent this issue? How to fix this bug? plz help me.
Is there any other way to lock and unlock ?
thanks in advance.

reenableKeyguard
void reenableKeyguard() Reenable the keyguard. The keyguard will
reappear if the previous call to disableKeyguard() caused it to be
hidden. A good place to call this is from onPause() Note: This call
has no effect while any DevicePolicyManager is enabled that requires a
password.
This method requires the caller to hold the permission
DISABLE_KEYGUARD.
Your app has to be the one to call disableKeyguard() first, else you cannot lock it using reenableKeyguard()
Source: https://developer.android.com/reference/android/app/KeyguardManager.KeyguardLock.html

Related

What is KeyguardLock disableKeyguard() for?

I have following handed-over existing code:
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
Based on here and here, is it to prevent the lock screen to appear? However, I am still able to lock my device. Can anyone advise me what is it for?
From the docs:
public void disableKeyguard()
Disable the keyguard from showing. If the keyguard is currently
showing, hide it. The keyguard will be prevented from showing again
until reenableKeyguard() is called. A good place to call this is from
Activity.onResume() Note: This call has no effect while any
android.app.admin.DevicePolicyManager is enabled that requires a
password.
This method requires the caller to hold the permission
android.Manifest.permission.DISABLE_KEYGUARD.
So it really depends on where exactly you put this snippet exactly, and it definitely not means that you can't see your lock screen anymore. In addition, if you have some security in your lock screen, this snippet can bypass the security measure.
It is used to unlock your screen programmatically. Maybe you also noticed while testing, your lockscreen won't appear until you call reenableKeyguard() again. Although reenableKeyguard() will only work if you also called disableKeyguard() from your application.
is it to prevent the lock screen to appear? However, I am still able to lock my device.
But you do see your lockscreen?
Can anyone advise me what is it for?
Opening a notification from the lockscreen basically does this. It disables your keyguard (lockscreen) and launches the app.

Unlock a screen in android using disableKeyguard

In my application I am using disableKeyguard and reenableKeyguard to Unlock and lock a screen. my application is working fine. But problem is when user unexpectedly unlocked a screen by dragging keygaurd (without using my application), the screen does't lock again with my application.
How to solve this problem?
Thanks in advace
Use FLAG_DISMISS_KEYGUARD and/or FLAG_SHOW_WHEN_LOCKED instead; this allows you to seamlessly hide the keyguard as your application moves in and out of the foreground and does not require that any special permissions be requested. Handle returned by newKeyguardLock(String) that allows you to disable / reenable the keyguard.

disablekeyguard() not working properly when receiver of SCREEN_ON is triggered in Android 2.2?

I am trying to disable the keyguard when SCREEN_ON is trigged. The following code is in a service run by the broadcast receiver of ACTION_SCREEN_ON:
KeyguardManager myKeyGuard = (KeyguardManager)getSystemService(Context.KEYGUARD_SERVICE);
myLock = myKeyGuard.newKeyguardLock();
myLock.disableKeyguard();
startActivity("xxx");
This seems to work, but there is a problem. After the screen is on and my activity is displayed bypassing the keyguard, if the user presses the home button, the lockscreen is displayed, and if the user unlocks the screen, the next time the screen switches on, the keyguard is not disabled. I have to manually unlock the phone, and the activity is indeed running behind the lockscreen. So only the disablekeyguard() seems to stop working.
Any help with be appreciated:)
Apparently there is ambiguity in how the Home key events handled by Android. The OP of a post here seems to be facing the same problem. No answers to his query yet.

Android Service Listener for the wakelock screen

Hii all,
Im developing an emergency calling application. What i want is when some person uses this specific code the phone will unlock and then only my application would be running. Im juz thinking i need a reciever for it and just wondering wether i will have to create my own Home screen and a lock screen for my application. any ideas on this please???
many thanks in advance :)
There's some caveats in just doing SCREEN_OFF and USER_PRESENT
1) Phone isn't locked right after screen off if the screen timed out by itself, there's a few second delay
2) If screen goes off for other reasons (phone call) it might not be locked at all.
3) You'd have to be monitoring them the whole time, if you're started when the phone is locked you wouldn't know
You can use the KeyguardManager http://developer.android.com/reference/android/app/KeyguardManager.html and check inKeyguardRestrictedInputMode()
Another option would be to use the PowerManager http://developer.android.com/reference/android/os/PowerManager.html and check isScreenOn() if you actually just care about screen state and not keyguard state.
You can create a BroadcastReceiver and register it with your application to listen for Intent.ACTION_SCREEN_OFF, Intent.ACTION_SCREEN_ON, and Intent.ACTION_USER_PRESENT. Between SCREEN_OFF and USER_PRESENT, the phone is locked.
There is no sanctioned way to replace the lock screen. See Is there a way to override the lock pattern screen?
The previous answer was to another question that got merged with this one:
I would look into ACTION_NEW_OUTGOING_CALL, and also at this Android Developers blog post about receiver priority.

Android WakeLock and KeyGuard

I'm just wondering if I do this correctly; I'm programming a notification app which can display a notification when the phone is sleeping
Disable keyguard lock
Aquire a wake lock
show notification
Set alarm for timeout and reenabling keyguard and release wakelock is the user dont touches the screen.
4.1 User touches the screen, and I disable the timer. Do nothing more. Done and done
4.2 User dont touch the screen, so reenable keyguard and release wakelock. Phone sleeps again
Basically I'm wondering about point 4.1 the most. cancel the pendingintent for the alarm, and do nothing more? or should the keyguard and wakelock that are set be dealt with in some way?
The trick to implement your own Keyguard-replacement appears to be the following:-
In the onCreate method, you don't disable the keyguard, but the user can interact with the screen at this point so you need to be careful about accidental touches.
getWindow().addFlags(LayoutParams.FLAG_SHOW_WHEN_LOCKED |
LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_KEEP_SCREEN_ON);
mKeyguardManager = (KeyguardManager)mContext.getSystemService(Context.KEYGUARD_SERVICE);
mKeyguardLock = mKeyguardManager.newKeyguardLock(TAG);
If the user performs some action to indicate they want to interact more fully, then we can disable the keyguard and move on.
mKeyguardLock.disableKeyguard();
mKeyguardManager.exitKeyguardSecurely(null);
If they don't, then since the keyguard isn't disabled you shouldn't need to do anything more, just finish your activity
Thats it, but I'm still testing it. So, I'm not 100% sure about it.
I know this question is old but the API clearly state that programatically trying to acquire a keyguard unlock is deprecated.
The correct strategy is, in the oncCreate method of your activity, to have:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
Hope this helps others.
Locks are global and when one appplication acquires it it will be off until the lock is released.
You should always reenable locks. Otherwise system won't go sleep or lock from Home or any other application
EDIT: I'm not really shure how it works with keyguard :/

Categories

Resources