How Android disables screen hibernation and lock screen

Source: Internet
Author: User
Tags tty mode

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
  1. Method One
  2. GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_ SCREEN_ON); Setcontentview (R.layout.main);
  3. Method Two
  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. @Override
  13. protected void OnPause () {
  14. Super.onpause ();
  15. if (null! = Mwakelock) {
  16. Mwakelock.release ();
  17. }
  18. }
  19. Method Three
  20. public void UnLock () {
  21. Mcontentresolver = Getcontentresolver ();
  22. //Not recommended for use
  23. //setlockpatternenabled (Android.provider.settings.system.lock_pattern_enabled,false);
  24. //Recommended Use
  25. Setlockpatternenabled (Android.provider.Settings.Secure.LOCK_PATTERN_ENABLED,false);
  26. }
  27. private void setlockpatternenabled (String systemsettingkey, Boolean enabled) {
  28. //Not recommended for use
  29. //android.provider.settings.system.putint (mcontentresolver,systemsettingkey, enabled 1:0);
  30. //Recommended Use
  31. Android.provider.Settings.Secure.putInt (Mcontentresolver, systemsettingkey,enabled?)  1: 0);
  32. }
  33. //But note to add permissions to the Androidmanifest.xml file
  34. //<uses-permission android:name= "Android.permission.WRITE_SETTINGS"/>
  35. //Special attention is to be added to the android:shareduserid= "Android.uid.system", but there is a problem,
  36. //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
  1. Private void Loadsystemsettings (Sqlitedatabase db) {
  2. Sqlitestatement stmt = db.compilestatement ("INSERT OR IGNORE into system (Name,value)"
  3. + "VALUES (?,?);");
  4. Resources r = mcontext.getresources ();
  5. Loadbooleansetting (stmt, Settings.System.DIM_SCREEN,
  6. R.bool.def_dim_screen);
  7. Loadsetting (stmt, Settings.System.STAY_ON_WHILE_PLUGGED_IN,
  8. "1". Equals (Systemproperties.get ("Ro.kernel.qemu"))?  1: 0);
  9. Loadintegersetting (stmt, Settings.System.SCREEN_OFF_TIMEOUT,
  10. R.integer.def_screen_off_timeout);
  11. //Set default CDMA emergency tone
  12. Loadsetting (stmt, Settings.System.EMERGENCY_TONE, 0);
  13. //Set default CDMA call Auto Retry
  14. Loadsetting (stmt, Settings.System.CALL_AUTO_RETRY, 0);
  15. //Set default CDMA DTMF type
  16. Loadsetting (stmt, Settings.System.DTMF_TONE_TYPE_WHEN_DIALING, 0);
  17. //Set default hearing aid
  18. Loadsetting (stmt, Settings.System.HEARING_AID, 0);
  19. //Set default TTY mode
  20. Loadsetting (stmt, Settings.System.TTY_MODE, 0);
  21. Loadbooleansetting (stmt, Settings.System.AIRPLANE_MODE_ON,
  22. R.BOOL.DEF_AIRPLANE_MODE_ON);
  23. Loadstringsetting (stmt, Settings.System.AIRPLANE_MODE_RADIOS,
  24. R.string.def_airplane_mode_radios);
  25. Loadstringsetting (stmt, Settings.System.AIRPLANE_MODE_TOGGLEABLE_RADIOS,
  26. R.string.airplane_mode_toggleable_radios);
  27. Loadbooleansetting (stmt, Settings.System.AUTO_TIME,
  28. R.bool.def_auto_time); //Sync time to NITZ
  29. Loadintegersetting (stmt, Settings.System.SCREEN_BRIGHTNESS,
  30. r.integer.def_screen_brightness);
  31. Loadbooleansetting (stmt, Settings.System.SCREEN_BRIGHTNESS_MODE,
  32. R.bool.def_screen_brightness_automatic_mode);
  33. Loaddefaultanimationsettings (stmt);
  34. Loadbooleansetting (stmt, Settings.System.ACCELEROMETER_ROTATION,
  35. R.bool.def_accelerometer_rotation);
  36. Loaddefaulthapticsettings (stmt);
  37. Stmt.close ();
  38. }

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
    1. <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
    1. /**
    2. * External apps (like the phone app) can tell us to disable the Keygaurd.
    3. */
    4. 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
  1. /**
  2. * Same semantics as {@link windowmanagerpolicy#enablekeyguard}; Provide
  3. * A-on-external stuff to override normal keyguard behavior. For instance
  4. * The phone app disables the Keyguard when it receives incoming calls.
  5. */
  6. Public void Setkeyguardenabled (Boolean enabled) {
  7. synchronized (this) {
  8. if (DEBUG) log.d (TAG, "setkeyguardenabled (" + enabled + ")");
  9. mexternallyenabled = enabled;
  10. if (!enabled && mshowing) {
  11. if (mexitsecurecallback! = null) {
  12. if (DEBUG) log.d (TAG, "in process of Verifyunlock request, ignoring");
  13. //we ' re in the process of handling a request to verify the user
  14. //Can get past the keyguard. Ignore extraneous requests to disable/reenable
  15. return;
  16. }
  17. //Hiding Keyguard that's showing, remember to Reshow later
  18. if (DEBUG) log.d (TAG, "Remembering to Reshow, hiding Keyguard,"
  19. + "Disabling status bar Expansion");
  20. mneedtoreshowwhenreenabled = true;
  21. Hidelocked ();
  22. } Else if (enabled && mneedtoreshowwhenreenabled) {
  23. ...
  24. ...
  25. }
  26. }

How Android disables screen hibernation and lock screen

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.