Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android系統取消自動鎖定屏幕

Android系統取消自動鎖定屏幕

編輯:Android開發實例

  鎖定屏幕對於移動終端來說是非常有必要的,但是對於機頂盒產品就沒有這個必要了。所以本文介紹一下怎樣讓Android設備永不鎖屏。     Android系統的鎖屏時間存放在Setting數據庫中,字段為Settings.System.SCREEN_OFF_TIMEOUT。查看SettingsProvider源碼,查看如下文件的源碼:frameworks/base/packages/SettingsProvider/src/com/android/providers/settings/DatabaseHelper.java

  查看loadSystemSettings()函數的代碼如下:

  private void loadSystemSettings(SQLiteDatabase db) {
SQLiteStatement stmt = db.compileStatement("INSERT OR IGNORE INTO system(name,value)"
+ " VALUES(?,?);");

  Resources r = mContext.getResources();

  loadBooleanSetting(stmt, Settings.System.DIM_SCREEN,
R.bool.def_dim_screen);
loadSetting(stmt, Settings.System.STAY_ON_WHILE_PLUGGED_IN,
"1".equals(SystemProperties.get("ro.kernel.qemu")) ? 1 : 0);
loadIntegerSetting(stmt, Settings.System.SCREEN_OFF_TIMEOUT,
R.integer.def_screen_off_timeout);

  // Set default cdma emergency tone
loadSetting(stmt, Settings.System.EMERGENCY_TONE, 0);

  // Set default cdma call auto retry
loadSetting(stmt, Settings.System.CALL_AUTO_RETRY, 0);

  // Set default cdma DTMF type
loadSetting(stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);

  // Set default hearing aid
loadSetting(stmt, Settings.System.HEARING_AID, 0);

  // Set default tty mode
loadSetting(stmt, Settings.System.TTY_MODE, 0);

  loadBooleanSetting(stmt, Settings.System.AIRPLANE_MODE_ON,
R.bool.def_airplane_mode_on);

  loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_RADIOS,
R.string.def_airplane_mode_radios);

  loadStringSetting(stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
R.string.airplane_mode_toggleable_radios);

  loadBooleanSetting(stmt, Settings.System.AUTO_TIME,
R.bool.def_auto_time); // Sync time to NITZ

  loadIntegerSetting(stmt, Settings.System.SCREEN_BRIGHTNESS,
R.integer.def_screen_brightness);

  loadBooleanSetting(stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
R.bool.def_screen_brightness_automatic_mode);

  loadDefaultAnimationSettings(stmt);

  loadBooleanSetting(stmt, Settings.System.ACCELEROMETER_ROTATION,
R.bool.def_accelerometer_rotation);

  loadDefaultHapticSettings(stmt);

  stmt.close();
}

  從代碼中我們可以看出,假如Settings.System.SCREEN_OFF_TIMEOUT沒有初始化的話(系統首次啟動,這個字段肯定是沒有初始化的),將會利用資源中的R.integer.def_screen_off_timeout來初始化。我們為了讓系統永不鎖屏,只需要把資源R.integer.def_screen_off_timeout設為-1即可。

  查看文件
frameworks/base/packages/SettingsProvider/res/values/defaults.xml
可以找到R.integer.def_screen_off_timeout的定義。
<integer name="def_screen_off_timeout">60000</integer>
發現默認值為60000ms,也就是60s。我們只需要把這個參數改為-1。然後重新編譯SettingsProvider模塊,就OK了。

  另外為了防止用戶進入系統後,修改鎖屏時間,在Setting模塊中刪除對鎖屏時間的設置。這樣Android設備就永不鎖屏了。

  鎖屏的代碼在LockScreen.java中,可以設置鎖屏功能開關的位置。代碼位於: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 [email protected] 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方法,關閉鎖屏功能。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved