1.onUserLeaveHint
Compared to the home key (home) and recent application of the key (App_switch), enter is very simple. The complex onkeydown can be implemented as follows:
@Override publicbooleanonKeyDown(int keyCode, KeyEvent event) { // KeyEvent.KEYCODE_HOME KeyEvent.KEYCODE_APP_SWITCH if (keyCode == KeyEvent.KEYCODE_BACK) { exitAction(); returntrue; else returnsuper.onKeyDown(keyCode, event); }
Home and App_switch are more complex, especially home keys, assuming no special settings. may be exploited by malicious applications.
So even if the home button is processed, the actual measurement will first enter the launcher, a period of time before the operation of our operations, presumably this is the system layer from a security perspective. Have time to look at the system source code.
After trying a variety of ways. The replication Onuserleavehint function is more convenient. For example, the following can be:
@Override protectedvoidonUserLeaveHint() { //super.onUserLeaveHint(); exitAction(); }
The operation is written in the Exitaction function, which takes the startup setting as an example:
privatevoidexitAction() { try { new Intent(); new ComponentName("com.android.settings", "com.android.settings.Settings"); intent.setComponent(componentName); startActivity(intent); catch (Exception e) { e.printStackTrace(); } }
2.disable_keyguard
Androidmanifest.xml Plus permissions:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
Activity handling:
Public class Main extends Activity { Public Static Final intflag_homekey_dispatched =0x80000000;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); This. GetWindow (). SetFlags (flag_homekey_dispatched, flag_homekey_dispatched); Setcontentview (R.layout.main); }@Override Public Boolean OnKeyDown(intKeyCode, KeyEvent event) {//Keyevent.keycode_home Keyevent.keycode_app_switch if(keycode = = Keyevent.keycode_back | | keycode = = keyevent.keycode_home)return true;Else return Super. OnKeyDown (KeyCode, event); }}
3.Frameworks Source Code Churn
The home Key,power Key in Android responds by default to the system's framework. What if you want your app to capture a response?
Home key is relatively simple and does not involve system source code changes:
getWindow().setFlags(FLAG_HOMEKEY_DISPATCHED, FLAG_HOMEKEY_DISPATCHED);
- Override this main Activity's OnKeyDown () method to implement the processing part of home key, and
returntrue;
Power key is a bit cumbersome. To change the source code:
1: Change the Interceptkeybeforequeueing method of the Phonewindowmanager.java, add after the participation line:
CaseKeyEvent.Keycode_power: {//Participation lineResult&=~action_pass_to_user;//Participation line//Add beginActivitymanager Activitymanager=(Activitymanager) Getsystemservice (Context.Activity_service); try{if(Activitymanager.Getrunningtasks (1).Get0).Topactivity.Getpackagename ().equals("Your_special_package_name")){//please Reset the Special package name.Result|=Action_pass_to_user; Xlog.DTAG,"Detect power key in special package, pass to user!")}}catch (NullPointerException e) {Xlog.DTAG,"Ingore a nullpointerexception ...");}//add End
2: Same within this branch, find the Else statement segment and find for example the following location increment
if (interceptPowerKeyUp(canceled || mPendingPowerKeyUpCanceled)) { // 參考行if(!((result& ACTION_PASS_TO_USER) == ACTION_PASS_TO_USER)){ //增加行result = (result & ~ACTION_WAKE_UP) | ACTION_GO_TO_SLEEP; //原有代码}}//增加行
Zhou Mushi's CSDN Blog
Copyright Notice: Original article, reproduced please specify the source: Http://blog.csdn.net/zhoumushui
Android Home Link, back main collection and response