Disclaimer: Resources are from Https://github.com/shaobin0604/Android-HomeKey-Locker, can be accessed directly from this address, or access my resources http://download.csdn.net/ detail/youshenxuzuo/8575529 download, long-term effective.
In project development, it may be necessary to block the Android virtual keys, where the return key is relatively easy to disable, the following two ways to solve:
@Override Public boolean onKeyDown (int keycode, keyevent event) { if (keycode = = keyevent.keycode_back) { return true;} Return Super.onkeydown (KeyCode, event);
Or
@Overridepublic void onbackpressed () {}
Next explain how to block the home key:
1, download the above resources, unzip the file;
2. Copy the Homekeylocker.java file to your project;
3.
<style name= "Overlaydialog" parent= "@android: Style/theme.dialog" > <item name= "Android: Windowbackground "> @android:color/transparent</item> <item name=" Android:windowframe "> @null </item> <item name= "Android:windownotitle" >true</item> <item name= "Android: Windowisfloating ">true</item> <item name=" Android:windowistranslucent ">false</item> <item name= "android:backgrounddimenabled" >false</item> </style>
Copy to your styles.xml file;
4, will
<uses-permission android:name= "Android.permission.SYSTEM_ALERT_WINDOW"/>
Copied to the Androidmanifest.xml file;
5. Add code to the activity file that you want to block:
Private Homekeylocker Mhomekeylocker;
public void OnCreate (Bundle parambundle) {<span style= "white-space:pre" ></span>mhomekeylocker = new Homekeylocker (); <span style= "White-space:pre" ></span>mhomekeylocker.lock (LoginActivity.this);}
protected void OnDestroy () {<span style= "White-space:pre" ></span>mhomekeylocker.unlock (); Mhomekeylocker = Null;super.ondestroy ();}
。
The 5 steps above can be implemented to disable the home key.
Measured available equipment:
Android 4.4.4
Samsung TAB4
- Android 4.4.2
- CM11 (Google Nexus S, Moto Defy)
- Android 4.0.3
- Android 4.0.4
- Android 2.3.5
Devices not available
Disable the Android system home key