方法一:android 4.0以后无法通过更改页面的类型来监听home键了。如果不改源码的情况下,下面的方式可以很好的监听到home键(前提是手机打系统日志,有些手机日志被关闭了(如华为荣耀)就无法监听了)。
01 | protected?void?onResume() |
03 | ???? ???super.onResume(); |
04 | ???? ???isTesting=true; |
05 | ???? ???new?CatchLogThread().start(); |
07 | class?CatchLogThread?extends?Thread {?? |
09 | ???? ?public?void?run() {?? |
10 | ???? ?? ??Process mLogcatProc =?null;?? |
11 | ???? ?? ??BufferedReader reader =?null;?? |
12 | ???? ?? ???String line;?? |
13 | ???? ?? ???while?(isTesting) {?? |
16 | ???? ?? ?? ?? ?? ??mLogcatProc = Runtime.getRuntime().exec(new?String[] {?"logcat","ActivityManager:I *:S"?});?? |
17 | ???? ?? ?? ?? ?? ??reader =?new?BufferedReader(newInputStreamReader(mLogcatProc.getInputStream()),2*1024);?? |
18 | ???? ?? ?? ?? ?? ??while?((line = reader.readLine()) !=?null) {?? |
19 | ???? ?? ?? ?? ?? ?? ???if?(line.indexOf("android.intent.category.HOME") >?0) {?? |
20 | ???? ?? ?? ?? ?? ?? ?? ?? ?isTesting =?false;?? |
21 | ???? ?? ?? ?? ?? ?? ?? ?? ?System.out.println("DDDDDDDDDDDDDDDDDDDDDDDDD"); |
22 | ???? ?? ?? ?? ?? ?? ?? ?? ?handler.sendMessage(handler.obtainMessage());? ? |
23 | ???? ?? ?? ?? ?? ?? ?? ??Runtime.getRuntime().exec("logcat -c"); |
24 | ???? ?? ?? ?? ?? ?? ?? ???break;?? |
25 | ???? ?? ?? ?? ?? ?? ??}? ? |
27 | ???? ?? ?? ?? ?}?catch?(Exception e) {?? |
28 | ???? ?? ?? ?? ?? ??e.printStackTrace();?? |
33 | Handler handler =?new?Handler() {?? |
34 | ???? ??public?void?handleMessage(android.os.Message msg) { |
36 | ???? ?? ???startActivity(new?Intent(MainActivity.this,MainActivity.class)); |
37 | ???? ?? ???Toast.makeText(MainActivity.this,?"I Have Home is pressed!!!!!",Toast.LENGTH_LONG).show(); |
38 | ???? ?? ???System.out.println("Home is Pressed+++++++++++++++++"); |
最后记得在AndroidManifest.xml中加读取日志权限:
<uses-permission android:name="android.permission.READ_LOGS"></uses-permission>
方法二:广播监听
01 | private?final?BroadcastReceiver homePressReceiver =?new?BroadcastReceiver() { |
02 | ??? ? ? ? ? ? ??final?String SYSTEM_DIALOG_REASON_KEY =?"reason"; |
03 | ??? ? ? ? ? ? ??final?String SYSTEM_DIALOG_REASON_HOME_KEY =?"homekey"; |
05 | ??? ? ? ? ? ? ??@Override |
06 | ??? ? ? ? ? ? ??public?void?onReceive(Context context, Intent intent) { |
07 | ??? ? ? ? ? ? ? ? ? ? ??String action = intent.getAction(); |
08 | ??? ? ? ? ? ? ? ? ? ? ??if?(action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) { |
09 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ??String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY); |
10 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ??if?(reason !=?null |
11 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??&& reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) { |
12 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? |
13 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??Log.i(TAG,"home_press"); |
14 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??? |
15 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ??} |
16 | ??? ? ? ? ? ? ? ? ? ? ??} |
?注册广播:
1 | IntentFilter homeFilter =?new?IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS); |
2 | ??? ? ? ? ? ? ??registerReceiver(homePressReceiver, homeFilter); |
注销广播:
1 | if?(homePressReceiver !=?null) { |
2 | ??? ? ? ? ? ? ? ? ? ? ??try?{ |
3 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ??unregisterReceiver(homePressReceiver); |
4 | ??? ? ? ? ? ? ? ? ? ? ??}?catch?(Exception e) { |
5 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ??Log.e(TAG, |
6 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??"unregisterReceiver homePressReceiver failure :" |
7 | ??? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??+ e.getCause()); |
8 | ??? ? ? ? ? ? ? ? ? ? ??} |
?
方法三:在android2.2到android3.2使用:一下代码就可以截取HOME事件:
1.??public?void?onAttachedToWindow()??
2.??{????
3.????????this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);???????
4.?????????super.onAttachedToWindow();????
5.??} ?
?
在AndroidManifest.xml里面加上权限<uses-permissionandroid:name="android.permission.SYSTEM_ALERT_WINDOW"/>
好了,这样子就可以截取HOME事件了。
?
但是在android4.0上面,使用如下代码,就会报错:
错误信息如下:
?java.lang.IllegalArgumentException: Window type can not be changedafter the window is added.
方法四:监听HOME事件
想不到啊,想不到,android4.0上面居然还有监听HOME事件的。其实也不是仅仅监听HOME事件的,他监听的是,activity离开的事件:
所以只需要重写activity下面的:就可以监听咯
@Override
???????? protected void onUserLeaveHint() {
???????????????? super.onUserLeaveHint();
???????????????? System.out.println("onUserLeaveHint");
}
团队的项目解决就用了一个比较213的方法,就是当判断用户是点击HOME的离开事件之后,添加这写语句:
Intent intent = new Intent(this,还是这个ACTIVITY的类名.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);