Introduction
Often we develop the program when we do not need the system to wake the system lock screen function, such as we do xxxnowtv or XXX player, such as the program, users sometimes watch TV or video when you do not want the system lock screen function to start, do not want to lock the frequency, However, the system in our watch TV or video when we come out of a lock screen interface to lock frequency pull, we also want to continue to see the words also to unlock, so good trouble, not what we want, then how do we do it, in fact very simple, I only talk about two of them
One: We just need to implement the code in the program. The code is as follows:
[Java]View Plaincopy
- Method One
- GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_ SCREEN_ON); Setcontentview (R.layout.main);
- Method Two
- @Override
- protected void Onresume () {
- Super.onresume ();
- Pmanager = ((PowerManager) Getsystemservice (Power_service));
- Mwakelock = Pmanager.newwakelock (powermanager.screen_bright_wake_lock
- | Powermanager.on_after_release, TAG);
- Mwakelock.acquire ();
- }
- @Override
- protected void OnPause () {
- Super.onpause ();
- if (null! = Mwakelock) {
- Mwakelock.release ();
- }
- }
- Method Three
- public void UnLock () {
- Mcontentresolver = Getcontentresolver ();
- //Not recommended for use
- //setlockpatternenabled (Android.provider.settings.system.lock_pattern_enabled,false);
- //Recommended Use
- Setlockpatternenabled (Android.provider.Settings.Secure.LOCK_PATTERN_ENABLED,false);
- }
- private void setlockpatternenabled (String systemsettingkey, Boolean enabled) {
- //Not recommended for use
- //android.provider.settings.system.putint (mcontentresolver,systemsettingkey, enabled 1:0);
- //Recommended Use
- Android.provider.Settings.Secure.putInt (Mcontentresolver, systemsettingkey,enabled?) 1: 0);
- }
- //But note to add permissions to the Androidmanifest.xml file
- //<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
- //Special attention is to be added to the android:shareduserid= "Android.uid.system", but there is a problem,
- //If you can't compile with eclipse after joining Shareduserid, be sure to compile it manually via Mm-b and install the APK into the emulator or device
Second: Disable the system's lock frequency function, this method is not recommended to use, only in the environment of the appropriate use can be, we just know so we can also achieve such a function is OK.
We know that the lock screen time of the Android system is stored in the setting database and the field is Settings.System.SCREEN_OFF_TIMEOUT. We can view Settingsprovider source code, see the following file source code as follows:
~/frameworks/base/packages/settingsprovider/src/com/android/providers/settings/databasehelper.java
The code to view the Loadsystemsettings () function is as follows
[Java]View Plaincopy
- 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 ();
- }
We know by looking at the source code, Settings.System.SCREEN_OFF_TIMEOUT is not initialized (the first time the system starts, this field is definitely not initialized), the system will take advantage of the resources in the R.integer.def_screen_off _timeout to initialize. In order for the system to never lock the screen, we only need to set the resource R.integer.def_screen_off_timeout-1. View the file here:
Frameworks/base/packages/settingsprovider/res/values/defaults.xml
You can find the definition of r.integer.def_screen_off_timeout.
[XHTML]View Plaincopy
- <integer name="def_screen_off_timeout">60000</integer>
A default value of 60000ms is found, which is 60s. We just need to change this parameter to-1. Then recompile the Settingsprovider module and it will be OK.
But sometimes this happens, when the user enters the system, modifies the lock screen time, in order to happen we have to remove the lock screen time setting in the setting module. So the Android device will not lock the screen.
We also have to deal with a situation here, is to let the system start we disable the lock screen function, very simple, we just put the system lock screen function of the initial default switch to change the following can be, as follows to find this class:
Frameworks/policies/base/phone/com/android/internal/policy/impl/keyguardviewmediator.java
One of the variables in the file is defined as follows:
[Java]View Plaincopy
- /**
- * External apps (like the phone app) can tell us to disable the Keygaurd.
- */
- Private Boolean mexternallyenabled = true
Mexternallyenabled is the key to managing whether a screen lock is turned on. The default is to open the screen lock, according to the note can know he is want the application to modify this
Value, we can change this value to false.
Sometimes we don't want to change this initial value, so let's see if this class provides a way to modify the value externally, not what we expected, but here's what this code looks like:
[Java]View Plaincopy
- /**
- * Same semantics as {@link windowmanagerpolicy#enablekeyguard}; Provide
- * A-on-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's 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) {
- ...
- ...
- }
- }
How Android disables screen hibernation and lock screen