Java 以编程方式将锁屏设置为“无”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22142940/
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
Set lockscreen to "None" programmatically?
提问by Ranhiru Jude Cooray
I have the requirement to disable the lock screen and set the lock screen type to "None". My device is rooted (can run with SU permission) + can run as a system application with system permissions (under /system/app).
我需要禁用锁定屏幕并将锁定屏幕类型设置为“无”。我的设备是 root 的(可以使用 SU 权限运行)+ 可以作为具有系统权限的系统应用程序运行(在 /system/app 下)。
I have tried a few things to no avail.
我已经尝试了几件事无济于事。
Try 1
尝试 1
This seems to be deprecated and not working.
这似乎已被弃用并且不起作用。
KeyguardManager manager = (KeyguardManager) this.getSystemService(KEYGUARD_SERVICE);
KeyguardLock lock = manager.newKeyguardLock("abc");
lock.disableKeyguard();
Try 2
尝试 2
This didn't work either.
这也不起作用。
- Mount system partition as writable
- Edit
/data/data/com.android.providers.settings/databases/settings.db
Execute the following SQL.
INSERT OR REPLACE INTO system (name, value) VALUES ('lockscreen.disabled', '1');
INSERT OR REPLACE INTO secure (name, value) VALUES ('lockscreen.disabled', '1');
- 将系统分区挂载为可写
- 编辑
/data/data/com.android.providers.settings/databases/settings.db
执行以下 SQL。
INSERT OR REPLACE INTO system (name, value) VALUES ('lockscreen.disabled', '1');
INSERT OR REPLACE INTO secure (name, value) VALUES ('lockscreen.disabled', '1');
Try 3
尝试 3
Rebooted the machine but still no luck.
重新启动机器,但仍然没有运气。
android.provider.Settings.Secure.putLong(mContentResolver, Settings.Secure.LOCK_PATTERN_ENABLED, false);`
android.provider.Settings.Secure.putLong(mContentResolver, "lockscreen.password_type", DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);`
android.provider.Settings.Secure.putLong(mContentResolver, "lockscreen.password_type_alternate", DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);
android.provider.Settings.Secure.putLong(mContentResolver, "lockscreen.disabled", true);
Is there anything else that I can try?
还有什么我可以尝试的吗?
Please note that I do not want to disable the keyguard only when application is running.
请注意,我不想仅在应用程序运行时禁用键盘保护。
回答by Solution
you have to declare this uses-permission on AndroidManifest:
你必须在 AndroidManifest 上声明这个使用权限:
<uses-permission android:name="android.permission.WAKE_LOCK" />
And in your code:
在你的代码中:
PowerManager powerManager = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Lock");
wakeLock.acquire();
when your application is destroyed or paused to release this lock using below:
当您的应用程序被销毁或暂停以释放此锁时,请使用以下命令:
wakeLock.release();
I suggested to call the acquireinside the onResume()of your activity and the releasein onPause().
我建议在您的活动的onResume()中调用获取,并在onPause() 中调用释放。
回答by exilit
After searching a little bit in the android sourcei stick now within a function called onPreferenceTreeClick
from the class ChooseLockGeneric
which seems to be called when the unlock method is chosen (in the preferences).
在 android源代码中搜索了一点之后,我现在坚持在onPreferenceTreeClick
从类调用的函数中,该函数ChooseLockGeneric
似乎在选择解锁方法时调用(在首选项中)。
In that method updateUnlockMethodAndFinish
is called which sets the unlock method. So maybe calling updateUnlockMethodAndFinish(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,true);
could be exactly what you want.
在该方法updateUnlockMethodAndFinish
中调用设置解锁方法。所以也许打电话updateUnlockMethodAndFinish(DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED,true);
可能正是你想要的。
I don't know whether this fit your needs and don't know whether this works (maybe there are any visibility problems or security mechanisms). I am just speculating.
我不知道这是否符合您的需求,也不知道这是否有效(可能存在任何可见性问题或安全机制)。我只是在猜测。
EDIT:
This could help also: startFragment(this,"com.android.settings.ChooseLockGeneric$ChooseLockGenericFragment",SET_OR_CHANGE_LOCK_METHOD_REQUEST, null);
回答by superb
You can try this:
你可以试试这个:
adb shell sqlite3 /data/system/locksettings.db "UPDATE locksettings SET value = '1' WHERE name = 'lockscreen.disabled'"
adb shell sqlite3 /data/system/locksettings.db "UPDATE locksettings SET value = '0' WHERE name = 'lockscreen.password_type'"
adb shell sqlite3 /data/system/locksettings.db "UPDATE locksettings SET value = '0' WHERE name = 'lockscreen.password_type_alternate'"
It works on my rooted Nexus 4.
它适用于我扎根的 Nexus 4。
回答by Yeung
I would like to explore exilitanswer. From the source, we finally get into this:
我想探索流亡的答案。从源头上,我们终于进入了这个:
mChooseLockSettingsHelper.utils().clearLock(false);
mChooseLockSettingsHelper.utils().setLockScreenDisabled(disabled);
The utils is the com.android.internal.widget.LockPatternUtils
object and we can somehow get it by reflection.
Here is the code:
utils 是com.android.internal.widget.LockPatternUtils
对象,我们可以通过反射以某种方式获取它。这是代码:
try{
Class lockPatternUtilsCls = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor lockPatternUtilsConstructor =
lockPatternUtilsCls.getConstructor(new Class[]{Context.class});
Object lockPatternUtils = lockPatternUtilsConstructor.newInstance(MyActivity.this);
Method clearLockMethod = lockPatternUtils.getClass().getMethod("clearLock", boolean.class);
Method setLockScreenDisabledMethod = lockPatternUtils.getClass().getMethod("setLockScreenDisabled", boolean.class);
clearLockMethod.invoke(lockPatternUtils, false);
setLockScreenDisabledMethod.invoke(lockPatternUtils, true);
Log.d(TAG, "set lock screen to NONE SUC");
}catch(Exception e){
Log.e(TAG, "set lock screen to NONE failed", e);
}
Well my testing app is with signed with platform key and a system app. I think it will be a must.
那么我的测试应用程序是用平台密钥和系统应用程序签名的。我认为这将是必须的。
Updated: Noticed that the methods arguments are changed on Android 6.
更新:注意到方法参数在 Android 6 上发生了变化。
回答by jmatocha
I tried modifying /data/system/locksettings.db as was suggested by @ByteHamster, with no luck.
我尝试按照@ByteHamster 的建议修改 /data/system/locksettings.db,但没有成功。
adb shell sqlite3 /data/system/locksettings.db "UPDATE locksettings SET value = '1' WHERE name = 'lockscreen.disabled'"
adb shell sqlite3 /data/system/locksettings.db "UPDATE locksettings SET value = '1' WHERE name = 'lockscreen.disabled'"
I then renamed the locksettings.db
to locksettings.db.old
and rebooted. The lockscreen was gone and Android rebuilt the locksettings.db
file itself.
然后我将其重命名locksettings.db
为locksettings.db.old
并重新启动。锁屏消失了,Android 重建了locksettings.db
文件本身。
I am using TWRP recoveryon a Samsung Galaxy S4.
我在三星 Galaxy S4 上使用TWRP 恢复。
回答by Satheesh
I have acheived in root tablet,
我已经在根平板电脑上实现了,
Below is my full procedure
以下是我的完整程序
adb push sqlite3 /sdcard/sqlite3
adb push sqlite3 /sdcard/sqlite3
adb shell
亚行外壳
su
苏
mount -o remount,rw /system
mount -o remount,rw /system
cp /sdcard/sqlite3 /system/xbin/sqlite3
cp /sdcard/sqlite3 /system/xbin/sqlite3
chmod 755 /system/xbin/sqlite3
chmod 755 /system/xbin/sqlite3
mount -o remount,ro /system
mount -o 重新挂载,ro /system
adb shell sqlite3 /data/system/locksettings.db
adb shell sqlite3 /data/system/locksettings.db
UPDATE locksettings SET value = '1' WHERE name = 'lockscreen.disabled'
UPDATE locksettings SET value = '1' WHERE name = 'lockscreen.disabled'
You can download the file from http://www.digitechhub.com/sqlite3
您可以从http://www.digitechhub.com/sqlite3下载该文件
回答by JerryGoyal
use adb shell locksettings clear --old xxxx
to instantly remove the lockscreen lock (even when the phone is in locked state)
xxxx
is the pattern number, refer below image.
Example: locksettings clear --old 1236
用于adb shell locksettings clear --old xxxx
立即解除锁屏锁定(即使手机处于锁定状态)
xxxx
是模式编号,请参阅下图。例子:locksettings clear --old 1236
To again set the pattern use: locksettings set-pattern 1236
要再次设置模式,请使用: locksettings set-pattern 1236
usage:
用法:
locksettings set-pattern [--old OLD_CREDENTIAL] NEW_PATTERN
locksettings set-pin [--old OLD_CREDENTIAL] NEW_PIN
locksettings set-password [--old OLD_CREDENTIAL] NEW_PASSWORD
locksettings clear [--old OLD_CREDENTIAL]
locksettings verify [--old OLD_CREDENTIAL]
locksettings set-disabled DISABLED
locksettings get-disabled