android Home事件汇总
方法一:android 4.0以后无法通过更改页面的类型来监听home键了。如果不改源码的情况下,下面的方式可以很好的监听到home键(前提是手机打系统日志,有些手机日志被关闭了(如华为荣耀)就无法监听了)。
01 protected void onResume()
02 {
03 super.onResume();
04 isTesting=true;
05 new CatchLogThread().start();
06 }
07 class CatchLogThread extends Thread {
08 @Override
09 public void run() {
10 Process mLogcatProc = null;
11 BufferedReader reader = null;
12 String line;
13 while (isTesting) {
14 try {
15 // 获取logcat日志信息
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 }
26 }
27 } catch (Exception e) {
28 e.printStackTrace();
29 }
30 }
31 }
32 };
33 Handler handler = new Handler() {
34 public void handleMessage(android.os.Message msg) {
35
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+++++++++++++++++");
39 };
40 };
最后记得在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";
04
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 }
17 }
18 };
注册广播:
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 }
9 }
方法三:在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);
/**
*** 判断应用是否有活跃的activity**
*/
public boolean isActive()
{
ActivityManager mActivityManager = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> rti = mActivityManager.getRunningTasks(1);
return rti.get(0).topActivity.getPackageName().contains(getPackageName());
}
--------------------------------------------------------------------------
public class HomeWatcherReceiver extends BroadcastReceiver {
private static final String LOG_TAG = "HomeReceiver";
private static final String SYSTEM_DIALOG_REASON_KEY = "reason";
private static final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
private static final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
private static final String SYSTEM_DIALOG_REASON_LOCK = "lock";
private static final String SYSTEM_DIALOG_REASON_ASSIST = "assist";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i(LOG_TAG, "onReceive: action: " + action);
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
// android.intent.action.CLOSE_SYSTEM_DIALOGS
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
Log.i(LOG_TAG, "reason: " + reason);
if (SYSTEM_DIALOG_REASON_HOME_KEY.equals(reason)) {
// 短按Home键
Log.i(LOG_TAG, "homekey");
}
else if (SYSTEM_DIALOG_REASON_RECENT_APPS.equals(reason)) {
// 长按Home键 或者 activity切换键
Log.i(LOG_TAG, "long press home key or activity switch");
}
else if (SYSTEM_DIALOG_REASON_LOCK.equals(reason)) {
// 锁屏
Log.i(LOG_TAG, "lock");
}
else if (SYSTEM_DIALOG_REASON_ASSIST.equals(reason)) {
// samsung 长按Home键
Log.i(LOG_TAG, "assist");
}
}
}
}