完全去掉android的屏幕锁
修改keyguardmediator.java
将这个改称false
private boolean mExternallyEnabled = false;
再在powermanagerservice.java中
修改
private Runnable mNotificationTask = new Runnable()

在policy.screenTurnedOff(why);后加上
policy.enableKeyguard(true);
----------------------------------------------------------------------------------------------------------------------------------------
如何让Android开机不锁屏

要想android在开机启动完成时不锁屏,而且不影响其他情况下的锁屏特性,只需修改KeyguardViewMediator.java,在onSystemReady方法中注释掉对doKeyguard的调用即可。KeyguardViewMediator.java代码目录

frameworks/policies/base/phone/com/android/internal/policy/impl/

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设备就永不锁屏了。
后来发现我的Android设备烧录好之后第一次启动永远不会锁屏,但是设备重启之后开机就进入锁屏状态,解锁之后就再也不会锁屏了(因为永远不超时)。看来“革命尚未成功,同志仍需努力”啊。
那么为什么启动之后没有进入锁屏状态呢?是不会系统有把超时锁屏的值给修改了呢?我通过sqlite3去查看settings.db的内容,发现超时锁屏的值仍然是-1。说明启动之后,系统并没有去数据库中查看屏幕超时锁屏的值,就直接锁屏了。
但是怎样才能开机之后不进入锁屏状态呢?这个是个非常费思量的问题。经过go,我知道锁屏的代码在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 {@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,则永远也不会进入锁屏界面。完全满足了我的需求,终于大功告成了。开心啊~!!

Android屏幕锁定代码,同时分享基于Rexsee通过js快速实现

Android锁屏时会先调用onPause();解锁时调用onResume,读入保存的应用程序的资源。如果运行程序时已经锁屏,应用程序会先调用onCreate(),然后onResume(),再则onPause()。

取消锁屏:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
KeyguardManager mKeyGuardManager = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock mLock = mKeyGuardManager.newKeyguardLock("自己Activity名字");
mLock.disableKeyguard();

也是相当的简单了,但基于Rexsee的API,可以通过一句话搞定。
1. 取消锁屏:window.setTimeout('rexseeKeyguard.disable();alert(\'自动解锁!\');',10000);
alert('请按电源键关屏再开屏看到锁屏画面,10秒后自动解锁。')
2. 启动锁屏:rexseeKeyguard.reEnable();

如下是源码

 

01/*
02* Copyright (C) 2011 The Rexsee Open Source Project
03*
04* Licensed under the Rexsee License, Version 1.0 (the "License");
05* you may not use this file except in compliance with the License.
06* You may obtain a copy of the License at
07*
08*      http://www.rexsee.com/CN/legal/license.html
09*
10* Unless required by applicable law or agreed to in writing, software
11* distributed under the License is distributed on an "AS IS" BASIS,
12* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13* See the License for the specific language governing permissions and
14* limitations under the License.
15*/
16  
17package rexsee.core.alarm; 
18  
19import rexsee.core.browser.JavascriptInterface; 
20import rexsee.core.browser.RexseeBrowser; 
21import android.app.KeyguardManager; 
22import android.app.KeyguardManager.KeyguardLock; 
23import android.content.Context; 
24  
25public class RexseeKeyguard implements JavascriptInterface { 
26  
27       private static final String INTERFACE_NAME = "Keyguard"; 
28       @Override 
29       public String getInterfaceName() { 
30               return mBrowser.application.resources.prefix + INTERFACE_NAME; 
31       
32       @Override 
33       public JavascriptInterface getInheritInterface(RexseeBrowser childBrowser) { 
34               return this; 
35       
36       @Override 
37       public JavascriptInterface getNewInterface(RexseeBrowser childBrowser) { 
38               return new RexseeKeyguard(childBrowser); 
39       
40  
41       private final Context mContext; 
42       private final RexseeBrowser mBrowser; 
43       private KeyguardLock mKeyguardLock = null; 
44  
45       public RexseeKeyguard(RexseeBrowser browser) { 
46               mBrowser = browser; 
47               mContext = browser.getContext(); 
48       
49       public RexseeKeyguard(Context context) { 
50               mBrowser = null; 
51               mContext = context; 
52       
53  
54       //JavaScript Interface 
55  
56       public void enable() { 
57               /* 
58               try { 
59                       DevicePolicyManager dpm = (DevicePolicyManager) mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 
60                       dpm.lockNow(); 
61               } catch (Exception e) { 
62                       mBrowser.exception(getInterfaceName(), e); 
63               
64               */ 
65       
66       public void reEnable() { 
67               if (mKeyguardLock != null) { 
68                       mKeyguardLock.reenableKeyguard(); 
69                       mKeyguardLock = null; 
70               
71       
72       public void disable() { 
73               KeyguardManager keyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); 
74               mKeyguardLock = keyguardManager.newKeyguardLock(""); 
75               mKeyguardLock.disableKeyguard(); 
76       
77  
78}


Android 禁止屏幕休眠和锁屏的方法

常常我们开 发程序的时候我们不需要系统唤醒系统锁屏功能,比如我们在做xxxNowTV或XXX播放器这样的程序,用户有时候在看电视或视频的时候不希望系统的锁屏 功能启动,既不想锁频,然而系统却在我们看电视或者视频的时候出来个锁屏的界面进行锁频拉,我们还要想继续看的话还要去解锁,这样好麻烦,不是我们想要 的,那我们该怎么做呢,其实很简单,我这里只讲其中的两种

 

 :我们只要在程序中用代码实现。代码如下:  

·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. //方法一   
  2. getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); setContentView(R.layout.main);   
  3. //方法二   
  4.     @Override  
  5.     protected void onResume() {  
  6.         super.onResume();  
  7.         pManager = ((PowerManager) getSystemService(POWER_SERVICE));  
  8.         mWakeLock = pManager.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK  
  9.                 | PowerManager.ON_AFTER_RELEASE, TAG);  
  10.         mWakeLock.acquire();  
  11.     }  
  12.       
  13.     @Override  
  14.     protected void onPause() {  
  15.         super.onPause();  
  16.           
  17.         if(null != mWakeLock){  
  18.             mWakeLock.release();  
  19.         }  
  20.     }  
  21. //方法三  
  22.     public void unLock(){  
  23.         mContentResolver = getContentResolver();  
  24.         //不建议使用  
  25.         //setLockPatternEnabled(android.provider.Settings.System.LOCK_PATTERN_ENABLED,false);  
  26.           
  27.         //推荐使用  
  28.         setLockPatternEnabled(android.provider.Settings.Secure.LOCK_PATTERN_ENABLED,false);  
  29.     }  
  30.       
  31.     private void setLockPatternEnabled(String systemSettingKey, boolean enabled) {  
  32.          //不建议使用  
  33.          //android.provider.Settings.System.putInt(mContentResolver,systemSettingKey, enabled ? 1 : 0);  
  34.           
  35.          //推荐使用  
  36.          android.provider.Settings.Secure.putInt(mContentResolver, systemSettingKey,enabled ? 1 : 0);  
  37.     }  
  38.     //但注意要加权限AndroidManifest.xml文件中加入  
  39.     //<uses-permission android:name="android.permission.WRITE_SETTINGS" />  
  40.     //还要特别注意的是要加入 android:sharedUserId="android.uid.system",但有一个问题,  
  41.     //如果加入了sharedUserId后就不能使用eclipse编译了,一定要手动通过 mm -B进行编译,然后把apk install到模拟器或设备中