代码位于:
frameworks/policies/base/phone/com/android/internal/policy/impl/KeyguardViewMediator.java
该文件中有一个变量定义如下:
* External apps (like the phone app) can tell us to disable the keygaurd.
private boolean mExternallyEnabled = true;
mExternallyEnabled是用来管理是否开启屏幕锁的关键。默认值是打开屏锁,根据注释可以知道他是希望应用程序来修改这个值。但是经过加打印信息发现开机的时候没有任何应用程序会修改它。修改这个值调用如下函数:
* Same semantics as {@link WindowManagerPolicy#enableKeyguard}; provide
* a way for external stuff to override normal keyguard behavior. For instance
* the phone app disables the keyguard when it receives incoming calls.
public void setKeyguardEnabled(boolean enabled) {
synchronized (this) {
if (DEBUG) Log.d(TAG, "setKeyguardEnabled(" + enabled + ")");
mExternallyEnabled = enabled;
if (!enabled && mShowing) {
if (mExitSecureCallback != null) {
if (DEBUG) Log.d(TAG, "in process of verifyUnlock request, ignoring");
// we're in the process of handling a request to verify the user
// can get past the keyguard. ignore extraneous requests to disable / reenable
return;
}
// hiding keyguard that is showing, remember to reshow later
if (DEBUG) Log.d(TAG, "remembering to reshow, hiding keyguard, "
+ "disabling status bar expansion");
mNeedToReshowWhenReenabled = true;
hideLocked();
} else if (enabled && mNeedToReshowWhenReenabled) {
// reenabled after previously hidden, reshow
if (DEBUG) Log.d(TAG, "previously hidden, reshowing, reenabling "
+ "status bar expansion");
mNeedToReshowWhenReenabled = false;
if (mExitSecureCallback != null) {
if (DEBUG) Log.d(TAG, "onKeyguardExitResult(false), resetting");
mExitSecureCallback.onKeyguardExitResult(false);
mExitSecureCallback = null;
resetStateLocked();
} else {
showLocked();
// block until we know the keygaurd is done drawing (and post a message
// to unblock us after a timeout so we don't risk blocking too long
// and causing an ANR).
mWaitingUntilKeyguardVisible = true;
mHandler.sendEmptyMessageDelayed(KEYGUARD_DONE_DRAWING, KEYGUARD_DONE_DRAWING_TIMEOUT_MS);
if (DEBUG) Log.d(TAG, "waiting until mWaitingUntilKeyguardVisible is false");
while (mWaitingUntilKeyguardVisible) {
try {
wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
if (DEBUG) Log.d(TAG, "done waiting for mWaitingUntilKeyguardVisible");
}
}
}
}
经过上面的讨论我们可以发现我们有两个解决方法:
1、定义变量的时候,给其初始化为false。
2、在launcher模块启动的时候,调用setKeyguardEnabled方法,关闭锁屏功能。
我懒得修改Laucher模块,我的解决方法就是在定义mExternallyEnabled时修改其初始值为false。各位朋友可以根据自己的实际情况选择解决方案。我的代码如下:
* External apps (like the phone app) can tell us to disable the keygaurd.
private boolean mExternallyEnabled = false;
这样修改之后,Android设备开机之后,默认不会进入锁屏状态,除非你在应用程序中调用setKeyguardEnabled方法显示打开这个功能。因为设置的超时时间为-1,则永远也不会进入锁屏界面。完全满足了我的需求,终于大功告成了。开心啊~!!