Android 拦截 HOME 按键
ZZFROM: http://aking86.iteye.com/blog/1317186
=================================
Android 拦截 HOME 按键
主要就是重写 onAttachedToWindow () 和 onKeyDown (...,...) 这两个方法。
前者是起到拦截作用的,后者是监听到HOME按键事件后的动作,如果没动作可以不要 onKeyDown。
还要注意,要加权限:
<uses-permission android:name="android.permission.DISABLE_KEYGUARD "></uses-permission>
// 转载请注明出处: http://aking86.iteye.com/admin/blogs/1317186
虽然网上有很多,但介绍的都不是很详细,也不算深入,我在这里研究了下,并把日志给捞了出来。
里面包括 HOME拦截, BACK拦截,打捞日志。
@Override
public void onAttachedToWindow () {
System.out.println("Page01 -->onAttachedToWindow");
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
public void onAttachedToWindow () {
System.out.println("Page01 -->onAttachedToWindow");
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
super.onAttachedToWindow();
}
@Override
public boolean onKeyDown (int keyCode, KeyEvent event) {
System.out.println("Page01 -->onKeyDown: keyCode: " + keyCode);
if (KeyEvent.KEYCODE_HOME == keyCode) {
System.out.println("HOME has been pressed yet ...");
// android.os.Process.killProcess(android.os.Process.myPid());
Toast.makeText(getApplicationContext(), "按了HOME 键...",
Toast.LENGTH_LONG).show();
} else if (KeyEvent.KEYCODE_BACK == keyCode) {
System.out.println("BACK has been pressed yet ..."); //按了返回
}
return super.onKeyDown(keyCode, event); // 不会回到 home 页面
}
public boolean onKeyDown (int keyCode, KeyEvent event) {
System.out.println("Page01 -->onKeyDown: keyCode: " + keyCode);
if (KeyEvent.KEYCODE_HOME == keyCode) {
System.out.println("HOME has been pressed yet ...");
// android.os.Process.killProcess(android.os.Process.myPid());
Toast.makeText(getApplicationContext(), "按了HOME 键...",
Toast.LENGTH_LONG).show();
} else if (KeyEvent.KEYCODE_BACK == keyCode) {
System.out.println("BACK has been pressed yet ..."); //按了返回
}
return super.onKeyDown(keyCode, event); // 不会回到 home 页面
}
通过打印日志,还得出一个结论:
onAttachedToWindow() 方法只会执行一次,也就是在第一次 onResume() 之后,以后就不再执行了。
下面是我打印的日志。包含了Activity的生命周期,查看起来很方便。操作流程也就是打开一个页面,先按下HOME,弹出提示,再按下一个Button,里面是 finish()方法。
[root ]# adb logcat -d -v time -s System.out:I
12-19 13:19:43.290 I/System.out( 824): Page01 -->onStart
12-19 13:19:43.290 I/System.out( 824): Page01 -->onResume
12-19 13:19:43.369 I/System.out( 824): Page01 -->onAttachedToWindow
12-19 13:19:50.959 I/System.out( 824): Page01 -->onKeyDown: keyCode: 3
12-19 13:19:50.959 I/System.out( 824): HOME has been pressed yet ...
12-19 13:22:17.999 I/System.out( 824): Page01 click to exit(finish)
12-19 13:22:18.079 I/System.out( 824): Page01 -->onPause
12-19 13:22:18.590 I/System.out( 824): Page01 -->onStop
12-19 13:22:18.590 I/System.out( 824): Page01 -->onDestroy
12-19 13:19:43.290 I/System.out( 824): Page01 -->onStart
12-19 13:19:43.290 I/System.out( 824): Page01 -->onResume
12-19 13:19:43.369 I/System.out( 824): Page01 -->onAttachedToWindow
12-19 13:19:50.959 I/System.out( 824): Page01 -->onKeyDown: keyCode: 3
12-19 13:19:50.959 I/System.out( 824): HOME has been pressed yet ...
12-19 13:22:17.999 I/System.out( 824): Page01 click to exit(finish)
12-19 13:22:18.079 I/System.out( 824): Page01 -->onPause
12-19 13:22:18.590 I/System.out( 824): Page01 -->onStop
12-19 13:22:18.590 I/System.out( 824): Page01 -->onDestroy
以下是进了第一个页面,再进第二个页面,再回到第一个页面,再按HOME按键。正常拦截到。
[root ]# adb logcat -d -v time -s System.out:I
12-19 13:42:09.900 I/System.out( 852): Page01 -->onStart
12-19 13:42:09.910 I/System.out( 852): Page01 -->onResume
12-19 13:42:09.969 I/System.out( 852): Page01 -->onAttachedToWindow
12-19 13:42:19.659 I/System.out( 852): Page01 click to page02
12-19 13:42:19.721 I/System.out( 852): Page01 -->onPause
12-19 13:42:19.990 I/System.out( 852): Page02 -->onStart
12-19 13:42:19.990 I/System.out( 852): Page02 -->onResume
12-19 13:42:20.430 I/System.out( 852): Page01 -->onStop
12-19 13:42:29.149 I/System.out( 852): Page02 click to page01 (finish)
12-19 13:42:29.229 I/System.out( 852): Page02 -->onPause
12-19 13:42:29.349 I/System.out( 852): Page01 -->onStart
12-19 13:42:29.349 I/System.out( 852): Page01 -->onResume
12-19 13:42:29.752 I/System.out( 852): Page02 -->onStop
12-19 13:42:29.752 I/System.out( 852): Page02 -->onDestroy
12-19 13:42:45.779 I/System.out( 852): Page01 -->onKeyDown: keyCode: 3
12-19 13:42:45.779 I/System.out( 852): HOME has been pressed yet ...
12-19 13:42:09.900 I/System.out( 852): Page01 -->onStart
12-19 13:42:09.910 I/System.out( 852): Page01 -->onResume
12-19 13:42:09.969 I/System.out( 852): Page01 -->onAttachedToWindow
12-19 13:42:19.659 I/System.out( 852): Page01 click to page02
12-19 13:42:19.721 I/System.out( 852): Page01 -->onPause
12-19 13:42:19.990 I/System.out( 852): Page02 -->onStart
12-19 13:42:19.990 I/System.out( 852): Page02 -->onResume
12-19 13:42:20.430 I/System.out( 852): Page01 -->onStop
12-19 13:42:29.149 I/System.out( 852): Page02 click to page01 (finish)
12-19 13:42:29.229 I/System.out( 852): Page02 -->onPause
12-19 13:42:29.349 I/System.out( 852): Page01 -->onStart
12-19 13:42:29.349 I/System.out( 852): Page01 -->onResume
12-19 13:42:29.752 I/System.out( 852): Page02 -->onStop
12-19 13:42:29.752 I/System.out( 852): Page02 -->onDestroy
12-19 13:42:45.779 I/System.out( 852): Page01 -->onKeyDown: keyCode: 3
12-19 13:42:45.779 I/System.out( 852): HOME has been pressed yet ...
Meet so Meet.
C plusplus
I-PLUS....