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 "/>
- Private BroadcastReceiver mScreenFilterReceiver = new BroadcastReceiver (){
- Public void onReceive (Context context, Intent intent ){
- If (intent. getAction (). equals (Intent. ACTION_SCREEN_ON )){
- // Handle the request
- } Else if (intent. getAction (). equals (Intent. ACTION_SCREEN_OFF )){
- }
- }
- };
2. Define IntentFilter in onCreate of Service and register consumer er
[Java]View plaincopy
- IntentFilter ScreenFilter = new IntentFilter ();
- ScreenFilter. addAction (Intent. ACTION_SCREEN_ON );
- ScreenFilter. addAction (Intent. ACTION_SCREEN_OFF );
- RegisterReceiver (mScreenFilterReceiver, ScreenFilter );
3. In onDestroy of the Service, you need to register the handler.
[Java]View plaincopy
- UnregisterReceiver (mScreenFilterReceiver );