"Android Code snippet eight" monitor whether the Android screen lock screen

Source: Internet
Author: User

Implementation method: 1) through Broadcastreceiver receive broadcast intent.action_screen_on and Intent.action_screen_off can determine whether the screen State lock screen, but only when the screen status changes will be issued broadcast ;

2) If you want to get the screen state before the screen state changes, you can call PowerManager's Isscreenon method through the reflection mechanism.

For specific implementations, see the code:

The implementation of Class screenobserver for screen state monitoring is implemented as follows:

[Java]View Plaincopy
  1. Package com.isoft.screen;
  2. Import Java.lang.reflect.Method;
  3. Import android.app.Activity;
  4. Import Android.content.BroadcastReceiver;
  5. Import Android.content.Context;
  6. Import android.content.Intent;
  7. Import Android.content.IntentFilter;
  8. Import Android.os.PowerManager;
  9. Import Android.util.Log;
  10. /**
  11. *
  12. * @author Zhangyg
  13. *
  14. */
  15. Public class screenobserver{
  16. private static String TAG = "Screenobserver";
  17. private Context Mcontext;
  18. private Screenbroadcastreceiver Mscreenreceiver;
  19. private Screenstatelistener Mscreenstatelistener;
  20. private static Method mreflectscreenstate;
  21. Public Screenobserver (context context) {
  22. Mcontext = context;
  23. Mscreenreceiver = new Screenbroadcastreceiver ();
  24. try {
  25. Mreflectscreenstate = PowerManager. Class.getmethod ("Isscreenon",
  26. new class[] {});
  27. } catch (Nosuchmethodexception nsme) {
  28. LOG.D (TAG, "API < 7," + nsme);
  29. }
  30. }
  31. /** 
  32. * Screen Status Broadcast recipient
  33. * @author Zhangyg
  34. *
  35. */
  36. private class Screenbroadcastreceiver extends broadcastreceiver{
  37. private String action = null;
  38. @Override
  39. public void OnReceive (context context, Intent Intent) {
  40. Action = Intent.getaction ();
  41. if (Intent.ACTION_SCREEN_ON.equals (ACTION)) {
  42. Mscreenstatelistener.onscreenon ();
  43. }Else if (Intent.ACTION_SCREEN_OFF.equals (ACTION)) {
  44. Mscreenstatelistener.onscreenoff ();
  45. }
  46. }
  47. }
  48. /** 
  49. * Request a screen status update
  50. * @param listener
  51. */
  52. public void Requestscreenstateupdate (Screenstatelistener listener) {
  53. Mscreenstatelistener = listener;
  54. Startscreenbroadcastreceiver ();
  55. Firstgetscreenstate ();
  56. }
  57. /** 
  58. * Request screen status for the first time
  59. */
  60. private void Firstgetscreenstate () {
  61. PowerManager manager = (PowerManager) mcontext
  62. . Getsystemservice (Activity.power_service);
  63. if (Isscreenon (manager)) {
  64. if (mscreenstatelistener! = null) {
  65. Mscreenstatelistener.onscreenon ();
  66. }
  67. } Else {
  68. if (mscreenstatelistener! = null) {
  69. Mscreenstatelistener.onscreenoff ();
  70. }
  71. }
  72. }
  73. /** 
  74. * Stop Screen status update
  75. */
  76. public void Stopscreenstateupdate () {
  77. Mcontext.unregisterreceiver (Mscreenreceiver);
  78. }
  79. /** 
  80. * Start Screen status broadcast receiver
  81. */
  82. private void Startscreenbroadcastreceiver () {
  83. Intentfilter filter = new Intentfilter ();
  84. Filter.addaction (intent.action_screen_on);
  85. Filter.addaction (Intent.action_screen_off);
  86. Mcontext.registerreceiver (mscreenreceiver, filter);
  87. }
  88. /** 
  89. * Screen is open
  90. * @param pm
  91. * @return
  92. */
  93. private static boolean Isscreenon (PowerManager pm) {
  94. Boolean screenstate;
  95. try {
  96. Screenstate = (Boolean) mreflectscreenstate.invoke (PM);
  97. } catch (Exception e) {
  98. Screenstate = false;
  99. }
  100. return screenstate;
  101. }
  102. Public interface Screenstatelistener {
  103. public void Onscreenon ();
  104. public void Onscreenoff ();
  105. }
  106. }


The example class screenobserveractivity that tests the Screenobserver feature is implemented as follows:

[Java]View Plaincopy
    1. Package com.isoft.screen;
    2. Import android.app.Activity;
    3. Import Android.os.Bundle;
    4. Import Android.util.Log;
    5. Import Com.isoft.screen.ScreenObserver.ScreenStateListener;
    6. Public class Screenobserveractivity extends Activity {
    7. private String TAG = "Screenobserveractivity";
    8. private Screenobserver Mscreenobserver;
    9. @Override
    10. public void OnCreate (Bundle savedinstancestate) {
    11. super.oncreate (savedinstancestate);
    12. Setcontentview (R.layout.main);
    13. Mscreenobserver = New Screenobserver (this);
    14. Mscreenobserver.requestscreenstateupdate (new Screenstatelistener () {
    15. @Override
    16. public void Onscreenon () {
    17. Dosomethingonscreenon ();
    18. }
    19. @Override
    20. public void Onscreenoff () {
    21. Dosomethingonscreenoff ();
    22. }
    23. });
    24. }
    25. private void Dosomethingonscreenon () {
    26. LOG.I (TAG, "screen was on ");
    27. }
    28. private void Dosomethingonscreenoff () {
    29. LOG.I (TAG, "screen is Off");
    30. }
    31. @Override
    32. protected void OnDestroy () {
    33. Super.ondestroy ();
    34. //Stop monitoring screen status
    35. Mscreenobserver.stopscreenstateupdate ();
    36. }
    37. }
    38. http://blog.csdn.net/m_changgong/article/details/7608911

"Android Code snippet eight" monitor whether the Android screen lock screen

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.