Android - 如何完全禁用键盘锁
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21852203/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - how to completely disable keyguard
提问by kowalski
I want to control enabling/disabling keyguard on the device. To do that I'm using DevicePolicyManager and KeyguardLock API of Android SDK.
我想控制启用/禁用设备上的键盘锁。为此,我使用 Android SDK 的 DevicePolicyManager 和 KeyguardLock API。
Below is my implementation to manage this:
下面是我管理这个的实现:
public class DeviceLocker {
private static DeviceLocker instance;
public static synchronized DeviceLocker getInstance(Context context) {
if(instance==null) {
instance = new DeviceLocker(context);
}
return instance;
}
private Context context;
private KeyguardLock lock;
private DeviceLocker(Context context) {
this.context = context;
}
public void lock() {
lock(true);
}
public void lock(boolean lockNow) {
getLock().reenableKeyguard();
DevicePolicyManager devicePolicyManager = getDevicePolicyManager();
if(devicePolicyManager==null) {
return;
}
LocalStorage storage = LocalStorage.from(context);
boolean result = devicePolicyManager.resetPassword(storage.getPassword(),
DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);
if(lockNow) {
devicePolicyManager.lockNow();
}
storage.setDeviceLocked(true);
}
public void unlock() {
DevicePolicyManager devicePolicyManager = getDevicePolicyManager();
if(devicePolicyManager==null) {
return;
}
devicePolicyManager.resetPassword("",0);
getLock().disableKeyguard();
LocalStorage.from(context).setDeviceLocked(false);
}
private KeyguardLock getLock() {
if(lock==null){
KeyguardManager kgManager = (KeyguardManager)context.getSystemService(Activity.KEYGUARD_SERVICE);
lock = kgManager.newKeyguardLock(Context.KEYGUARD_SERVICE);
}
return lock;
}
private DevicePolicyManager getDevicePolicyManager() {
DevicePolicyManager devicePolicyManager =
(DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
ComponentName deviceAdmin = new ComponentName(context, WatchGuardDeviceAdminReceiver.class);
LocalStorage storage = LocalStorage.from(context);
if(!devicePolicyManager.isAdminActive(deviceAdmin)) {
return null;
}
if(!storage.isPasswordSet()) {
UIUtils.showMessage(context, R.string.password_not_set);
return null;
}
devicePolicyManager.setPasswordQuality(deviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
return devicePolicyManager;
}
}
}
It works ok regarding the lock of the screen, but unlock fucntionality works with some issues: sometimes it works as I want(completely removes any type of keyguard screen), but sometimes it shows "Unlock with slide" keyguard screen.
它在锁定屏幕方面工作正常,但解锁功能会遇到一些问题:有时它可以按我的意愿工作(完全删除任何类型的键盘锁屏),但有时它会显示“滑动解锁”键盘锁屏。
Do you know what is the problem here? How to make it work stable (at least in all cases either show "Unlock to slide" or completely remove keyguard)?
你知道这里有什么问题吗?如何使其工作稳定(至少在所有情况下显示“解锁滑动”或完全移除键盘锁)?
Thanks in advance for help.
预先感谢您的帮助。
EDIT
编辑
Just want to point out that my solution works but the problem is that it works unstable (sometimes removes keyguard completely and sometimes show "slide" keyguard).
And also I use it not just to disable keyguard while showing some activity but to control lock/unlock of device in common, so I use this code in Service, therefore I can't call getWindow().addFlags(..)
cause I don't have the Window to apply.
只是想指出我的解决方案有效,但问题是它的工作不稳定(有时会完全删除键盘保护,有时会显示“滑动”键盘保护)。而且我使用它不仅在显示某些活动时禁用键盘保护,而且还用于共同控制设备的锁定/解锁,因此我在服务中使用此代码,因此我无法调用,getWindow().addFlags(..)
因为我没有要应用的窗口。
Just wondering maybe anyone dealt with this unstable behavior.
只是想知道也许有人处理过这种不稳定的行为。
回答by Ivan Bartsov
AFAIK it's not universally possible, otherwise this would be a major security breach.
AFAIK 这不是普遍可能的,否则这将是一个重大的安全漏洞。
On a non-rooted phone the most you can do is dismiss non-secure keyguard or show your app above the secure keyguard.
在无根电话上,您最多可以关闭非安全键盘锁或在安全键盘锁上方显示您的应用程序。
Here's a combo to do this:
这是一个组合来做到这一点:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
On a rooted phone you can attempt to remove secure keyguard (although you may have to reboot the device before it actually goes away).
在有 root 权限的手机上,您可以尝试删除安全键盘锁(尽管您可能必须在它真正消失之前重新启动设备)。
Here's a combo for that (haven't tried it myself, but I'm pretty sure several "unlock your phone" apps in Play Store use this trick)
这是一个组合(我自己没有尝试过,但我很确定 Play 商店中的几个“解锁手机”应用程序都使用了这个技巧)
Runtime.getRuntime().exec("sudo rm /data/system/gesture.key");
I recommend you try this one through adb first. Also, here's an XDA thread with some more info on this: http://forum.xda-developers.com/showthread.php?t=1800799
我建议你先通过 adb 试试这个。此外,这里有一个 XDA 线程,其中包含更多信息:http: //forum.xda-developers.com/showthread.php?t=1800799
回答by dooplaye
You need to add this code:
您需要添加以下代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_FULLSCREEN|
WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
super.onCreate(savedInstanceState);
setContentView(R.layout.lockscreen);
startService(new Intent(this,LockService.class).setAction(Intent.ACTION_SCREEN_OFF));
}
Create LockService with:
使用以下命令创建 LockService:
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
if(intent == null || intent.getAction() == null)
return START_NOT_STICKY;
String action = intent.getAction();
if(action.equals(ACTION_SCREEN_OFF))
{
KeyguardManager.KeyguardLock k1;
KeyguardManager km =(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
k1= km.newKeyguardLock("IN");
k1.disableKeyguard();
}
return START_NOT_STICKY;
}
Actually i don't know why ,but k1.disableKeyguard();
works only in Service.
其实我不知道为什么,但k1.disableKeyguard();
只适用于服务。
回答by Swapnil
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
or
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
回答by Hassaan Rabbani
i would recommend using the window flags dismiss_keyguard or show_when_locked if you have a window that needs to come over the top of the lockscreen right at wakeup.
如果您有一个窗口需要在唤醒时越过锁屏顶部,我会建议使用窗口标志dismiss_keyguard 或show_when_locked。
how you use this is as follows (called in onCreate before setting layout)
你如何使用它如下(在设置布局之前在onCreate中调用)
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);