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.
Related
I am working on an application that will replace the default lock screen (swipe to unlock) for android devices. I have successfully done this by disabling the keyguard manager and showing my activity using the broadcast receiver for screen OFF and screen ON intent. Now, the problem is when I set the default screen lock again for any reason then my application would not disable the keyguard unless I force close it and launch it again.
km = (KeyguardManager) getApplicationContext().getSystemService(Context.KEYGUARD_SERVICE);
if( km.inKeyguardRestrictedInputMode()) {
//it is locked
km = (KeyguardManager) getApplicationContext().getSystemService(KEYGUARD_SERVICE);
kl=km.newKeyguardLock("com.example.helloworld.MainActivity");
kl.disableKeyguard();
} else {
Intent i = getIntent();
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(i);
}
You cannot replace the lock screen with a user application. Anything you do is a hack and may or may not work on some device, and will likely break with new releases. You can create something that looks like a screen lock, but it won't work like one. Additionally, in recent versions of Android (post-ICS), unlocking the screen does extra things like unlocking the credential storage, which your app cannot possibly do (since it doesn't have system permissions).
If you really want to replace the screen lock, you need to build your own Android ROM, modifying/replacing the stock one.
The accepted answer may be out of date.
It's now possible to use Device Admin to create and remove device passwords.
An application can be placed above the current lock screen using FLAG_SHOW_WHEN_LOCKED (complete explanation in another answer)
The keyguard can be dismissed using FLAG_DISMISS_KEYGUARD
As a result, it's theoretically possible to secure the actual lock screen using an app-generated password (providing real security), float a custom lock screen above the android lock screen, and -- when a proper password is provided -- unlock and dismiss the real lock screen. Finally, you would use a receiver to restore or clear the password on relevant events like SCREEN_OFF or SCREEN_ON -- the latter could automatically clear the password if a timeout was not yet reached.
FWIW, I don't recommend this approach since a crash or uninstall would leave a user with a device locked by a password they do not know.
Hey Im writing a launcher, in which Im building my own custom lockscreen.
the custom lockscreen is an activity which being launched whenever
the screen is off (by listening to Intent.ACTION_SCREEN_OFF)
to disable android's lockscreen I use this code:
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
It all works good, till some point, in which android's lockscreen is
turned on again (like it has never beem disabled before).
It happens a lot on samsung galaxy 2 (but happens also on other phones).
What Im I doing wrong?
thanks!
have you enabled DevicePolicyMangnager? if not that function won't work.
this comes from the documentation:
Note: This call has no effect while any DevicePolicyManager is enabled
that requires a password.
here is a tutorial about it. Anyway i think it's not what you are looking for cause anytime your app will do that the user will have to give a password to give you admin permissions.
in general there are no way to programmatically disable the KeyGuard ( if secure pin, password, pattern, face ) and the most you can do is to show an activity on top of it.
Managed to solve it somehow by calling
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
right before I start the lock screen activity
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
How can i check from my android application whether there is a keyguard enabled right now? I mean normally when screen goes off Android enables the keyguard, so next time you wake up you have keyguard screen infront of you. So I just want to know from the application that if there is a key guard enabled or not.
I had a look on keyguardmanager.java but it does not have that API.
Get the KeyGuardManager system service, and call inKeyguardRestrictedInputMode() function.
See Documentation Here.
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
Log.v(TAG,""+keyguardManager.inKeyguardRestrictedInputMode());
I tested on my HTC G1, it works.
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 :/