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 );}}


:




Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.