BroadcastReceiver: An Example of realizing screen lock and unlocking, broadcastreceiver

Source: Internet
Author: User

BroadcastReceiver: An Example of realizing screen lock and unlocking, broadcastreceiver

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.


So our breakthrough is: We can dynamically register a broadcaster about the unlocked screen (ACTION_USER_PRESENT), and implement some operations in the onReceive method of the broadcast. For example, we can start a Service to register the Broadcast Receiver we want.


1. Define worker er in Service

[Java]View plaincopy
  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 );


In order to unlock the screen with one click, do not press the power button. I installed an android light lock screen to unlock the screen, so that it can be put into the black screen of the pocket.

More advanced things are more fun !!
 
How does one implement lock screen unlocking for ios listeners? How does one call it?

First of all, if you don't know about ios, you dare to mention this feature. I really admire it.

Generally, the application will give you a short time to process tasks when you switch to the background. Music, GPS, and other applications will have a special extended time so that they will not be killed, but it is not feasible to do such things in the background as you need.

You can monitor whether an application enters the background, but the system-level listening lockscreen and unlocking are unavailable.
If there is a way to deal with what you call, it is also something that the jailbreak system can do, but even if you want to implement this technology, you are not just an application programmer.
The content I mentioned above is time-sensitive and does not guarantee that the IOS system will not be available in the future.

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.