How does Android disable screen sleep and lock screen?

Source: Internet
Author: User

Introduction

 

We often launchProgramWe do not need to wake up the screen lock function of the system. For example, if we are running a program such as xxxnowtv or XXX player, users sometimes do not want the screen lock function to start when watching TV or video, I don't want to lock the frequency, but the system is locking the screen when we watch TV or video. We need to unlock the screen if we want to continue watching the screen, which is troublesome, what should we do if we don't want it? It's actually very simple. Here I only talk about two of them.

 

I: We only need to useCode. The Code is as follows:

 

[Java: showcolumns: firstline [1] View plaincopy

··· · 50 ······· · 90 ····· · 140 · 150
  1. // Method 1
  2. Getwindow (). setflags (windowmanager. layoutparams. flag_keep_screen_on, windowmanager. layoutparams. flag_keep_screen_on); setcontentview (R. layout. Main );
  3. // Method 2
  4. @ Override
  5. Protected VoidOnresume (){
  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 VoidOnpause (){
  14. Super. Onpause ();
  15. If(Null! = Mwakelock ){
  16. Mwakelock. Release ();
  17. }
  18. }
  19. // Method 3
  20. Public VoidUnlock (){
  21. Mcontentresolver = getcontentresolver ();
  22. // Not recommended
  23. // Setlockpatternenabled (Android. provider. settings. system. lock_pattern_enabled, false );
  24. // Recommended
  25. Setlockpatternenabled (Android. provider. settings. Secure. lock_pattern_enabled,False);
  26. }
  27. Private VoidSetlockpatternenabled (string systemsettingkey,BooleanEnabled ){
  28. // Not recommended
  29. // Android. provider. settings. system. putint (mcontentresolver, systemsettingkey, enabled? 1: 0 );
  30. // Recommended
  31. Android. provider. settings. Secure. putint (mcontentresolver, systemsettingkey, enabled?1:0);
  32. }
  33. // Add the permission to the androidmanifest. xml file
  34. // <Uses-Permission Android: Name = "android. Permission. write_settings"/>
  35. // Note that you need to add Android: shareduserid = "android. uid. System", but there is a problem,
  36. // If shareduserid cannot be compiled using eclipse, you must manually compile it using mm-B and then install the APK to the simulator or device.

 

 

Ii. disable the system lock function. This method is not recommended. It can be used only in the configured environment, as long as we know that this way we can achieve this function implementation, it is OK.

We know that the lock time of the Android system is stored in the setting database, and the field is settings. system. screen_off_timeout. You can view the settingsprovider source code. The source code of the following file is as follows:

 
~ /Frameworks/base/packages/settingsprovider/src/COM/Android/providers/settings/databasehelper. Java
The code for viewing the loadsystemsettings () function is as follows:

 

[Java: collapse: showcolumns: firstline [1] + Expand sourceview plaincopy

··· · 50 ······· · 90 ····· · 140 · 150

 

We can see through the source code, If settings. system. screen_off_timeout is not initialized (This field is certainly not initialized when the system is started for the first time), the system will use R. Integer. def_screen_off_timeout in the resource for initialization. To keep the system locked, set resource R. Integer. def_screen_off_timeout to-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: showcolumns: firstline [1] View plaincopy

··· · 50 ······· · 90 ····· · 140 · 150
    1. <Integer Name="Def_screen_off_timeout">60000</Integer>

 

The default value is 60000 ms, that is, 60 s. We only need to change this parameter to-1. Then re-compile the settingsprovider module.

However, sometimes this happens. After the user enters the system, the lock screen time is modified. In this case, we have to delete the lock screen time setting in the setting module. In this way, the Android device will not be locked.

We also need to deal with a situation here, that is, to enable the screen lock function as soon as the system is started, it is very simple, we just need to lock the system Screen You can change the default switch of the function to the following:

 
Frameworks/policies/base/phone/COM/Android/Internal/policy/impl/keyguardviewmediator. Java
The file contains a variable defined as follows:
[Java: showcolumns: firstline [1] View plaincopy

··· · 50 ······· · 90 ····· · 140 · 150
    1. /** 
    2. * External apps (like the phone app) can tell us to disable the keygaurd. 
    3. */
    4. Private BooleanMexternallyenabled =True
 

Mexternallyenabled is the key to managing whether to enable screen lock. The default value is to enable the screen lock. According to the notes, you can know that you want the application to modify the screen lock.
Value. You can change this value to false.
Sometimes we don't want to modify this initial value, so let's see if this class provides the corresponding method for external modification of this value. As we can't expect, let's look at the following code:
[Java: showcolumns: firstline [1] View plaincopy

··· · 50 ······· · 90 ····· · 140 · 150
  1. /** 
  2. * Same semantics as {@ link windowmanagerpolicy # enablekeyguard}; Provide 
  3. * A way for external stuff to override normal keyguard behavior. For instance 
  4. * The phone app disables the keyguard when it has es incoming CILS. 
  5. */
  6. Public VoidSetkeyguardenabled (BooleanEnabled ){
  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 is 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. }
 
We can call this function to modify the value where we need to modify it. It's quite simple. OK, the lock frequency will be mentioned here. In fact, there are many knowledge points in the lock screen.
If you want to have a deeper understanding of the lock frequency, you can try to develop a simple screen program.
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.