BroadcastReceiver: An Example of screen lock and unlock

Source: Internet
Author: User

BroadcastReceiver: An Example of screen lock and unlock

I haven't written a small example of android for a long time, because I wrote an article about Intent a few days ago. action Article (http://blog.csdn.net/ljphhj/article/details/38796739), a friend of the private letter asked me about ACTION_SCREEN_ON and ACTION_SCREEN_OFF and ACTION_USER_PRESENT three Action usage, as a summary blog, at that time did not elaborate,ACTION_SCREEN_ON and ACTION_SCREEN_OFF can only be registered dynamically (context. register and unregister in the Code), while ACTION_USER_PRESENT can be registered dynamically or statically.Let's take a look at this screen lock and unlock related BroadcastReceiver.


Package cn. panghu. activitys; import com. example. broadcastsappdemo. r; import android. app. activity; import android. app. keyguardManager; import android. app. keyguardManager. keyguardLock; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; import android. OS. powerManager; import android. util. log; import android. view. view; import android. view. view. onClickListener; import android. view. viewGroup. layoutParams; import android. widget. button; import android. widget. linearLayout; import android. widget. textView; import android. widget. toast; public class ScreenLockedActivity extends Activity {private ScreenBroadcastReceiver Limit = null; private Context context = null; private Button lockedScreenBtn = null; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); context = getApplicationContext (); setContentView (R. layout. screen_lock_layout);} @ Overrideprotected void onResume () {// TODO Auto-generated method stubsuper. onResume (); // register the broadcast receiver ();} private void receiver () {screenBroadcastReceiver = new ScreenBroadcastReceiver (); IntentFilter intentFilter = new IntentFilter (); intentFilter. addAction (Intent. ACTION_SCREEN_OFF); // triggers intentFilter when the screen is locked. addAction (Intent. ACTION_SCREEN_ON); // triggers intentFilter when the screen is unlocked. addAction (Intent. ACTION_USER_PRESENT); // The context is triggered when the user wakes up the handheld device again. registerReceiver (screenBroadcastReceiver, intentFilter); Log. I ("screenBR", "screenBroadcastReceiver registered");} // rewrite broadcast class extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {String strAction = intent. getAction (); if (Intent. ACTION_SCREEN_OFF.equals (strAction) {// screen lock Log. I ("screenBR", "screen lock: ACTION_SCREEN_OFF trigger"); Toast. makeText (context, "locked", Toast. LENGTH_SHORT ). show ();} else if (Intent. ACTION_SCREEN_ON.equals (strAction) {// screen unlock (the actual test result cannot be used to determine the unlock screen event) // [This is triggered when the screen is unlocked, the broadcast is not registered at the time of unlocking.] Log. I ("screenBR", "screen unlock: ACTION_SCREEN_ON trigger"); Toast. makeText (context, "Unlocked", Toast. LENGTH_SHORT ). show ();} else if (Intent. ACTION_USER_PRESENT.equals (strAction) {// unlock the screen (this Action can be registered through static registration) // broadcast registered logs triggered after unlocking. I ("screenBR", "screen unlock: ACTION_USER_PRESENT trigger"); Toast. makeText (context, "Unlocked", Toast. LENGTH_SHORT ). show ();} else {// nothing }}@ Overrideprotected void onPause () {// TODO Auto-generated method stubsuper. onPause (); context. unregisterReceiver (screenBroadcastReceiver); Log. I ("screenBR", "screenBroadcastReceiver canceled registration ");}}


LogCat result diagram:



Because it is a static registration method, you may think that how can I make it listen to the screen lock and unlock the screen broadcast for a long time?

First, we emphasize that ACTION_SCREEN_ON and ACTION_SCREEN_OFF can only be dynamically registered (context. register and unregister in the Code), while ACTION_USER_PRESENT can be dynamically or statically registered. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Environment + PHA + Environment/environment/ydLUtq/environment/Environment + Environment/WztLDx9Kq1/a1xNK70Kmy2df3oaPA/cjnztLDx7/J0tS/Tables/modules + c/Environment + PHA + Environment" http://www.bkjia.com/uploads/allimg/140829/04533T040-1.png "width =" 12 "height =" 12 "alt =" view CODE slices on CODE "/>

  1. Private BroadcastReceiver mScreenFilterReceiver = new BroadcastReceiver (){
  2. Public void onReceive (Context context, Intent intent ){
  3. If (intent. getAction (). equals (Intent. ACTION_SCREEN_ON )){
  4. // Handle the request
  5. } Else if (intent. getAction (). equals (Intent. ACTION_SCREEN_OFF )){
  6. }
  7. }
  8. };

    2. Define IntentFilter in onCreate of Service and register consumer er

    [Java]View plaincopy
    1. IntentFilter ScreenFilter = new IntentFilter ();
    2. ScreenFilter. addAction (Intent. ACTION_SCREEN_ON );
    3. ScreenFilter. addAction (Intent. ACTION_SCREEN_OFF );
    4. RegisterReceiver (mScreenFilterReceiver, ScreenFilter );

      3. In onDestroy of the Service, you need to register the handler.

      [Java]View plaincopy
      1. UnregisterReceiver (mScreenFilterReceiver );

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.