Android shield & amp; quot; power key long press & amp; quot;, & amp; quot; Home Key & amp; quot;, & amp; quot; Home key long press & amp; quot;

Source: Internet
Author: User

In Android, Press & quot; power key long & quot;, & quot; Home Key & quot;, and & quot; Home key long press & quot;

In Android, shield "power key long press", "Home Key", and "Home key long press"


The trigger of these "buttons" will generate a notification of Action = Intent. ACTION_CLOSE_SYSTEM_DIALOGS.

The parameters for distinguishing the three different events are corresponding to the reason strings brought by Intent.

"Globalscreen", "homekey", and "Home Key" (recentapps)

It's that simple to say, and you can directly access the code.

Package com. jumpinus. test; import android. app. activity; import android. content. broadcastReceiver; import android. content. context; import android. content. intent; import android. content. intentFilter; import android. OS. bundle; public class MainActivity extends Activity {private MyBroadCastReceiver myBroadCastReceiver; @ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method s Tubsuper. onCreate (savedInstanceState); myBroadCastReceiver = new MyBroadCastReceiver () ;}@ Overrideprotected void onResume () {// TODO Auto-generated method stubsuper. onResume (); IntentFilter intentFilter = new IntentFilter (); intentFilter. addAction (Intent. ACTION_CLOSE_SYSTEM_DIALOGS); System. out. println ("I'm coming, myBroadCastReceiver registered! "); RegisterReceiver (myBroadCastReceiver, intentFilter);} private class MyBroadCastReceiver extends BroadcastReceiver {@ Overridepublic void onReceive (Context context, Intent intent) {// you should first exclude reasons = homekey and long-pressed homekey, and the rest will do the following processing String reason = intent. getStringExtra ("reason"); if (intent. getAction (). equals (Intent. ACTION_CLOSE_SYSTEM_DIALOGS) {System. out. println ("Intent. ACTION_CLOSE_SYSTEM_DIALOGS: "+ Intent. getStringExtra (" reason "); if (intent. getExtras ()! = Null & intent. getExtras (). getBoolean ("myReason") {myBroadCastReceiver. abortBroadcast ();} else if (reason! = Null) {if (reason. equalsIgnoreCase ("globalactions") {// Method for shielding long power keys: Intent myIntent = new Intent (Intent. ACTION_CLOSE_SYSTEM_DIALOGS); myIntent. putExtra ("myReason", true); context. sendOrderedBroadcast (myIntent, null); System. out. println ("power key long pressed");} else if (reason. equalsIgnoreCase ("homekey") {// Method for shielding the Home Key // here do some operations you want, such as re-opening your screen lock program interface, in this way, the System will not disappear. out. println ("Home Key triggered");} else if (reason. invalid sign OreCase ("recentapps") {// method of shielding Home key long press System. out. println ("Home Key pressed") ;}}}@ Overrideprotected void onPause () {// TODO Auto-generated method stubsuper. onPause (); System. out. println ("I get out, myBroadCastReceiver canceled! "); UnregisterReceiver (myBroadCastReceiver );}}


:




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.