原文链接:http://www.itnose.net/detail/6115323.html
这个参考了太多,算转载吧,我也不知道怎么样才算原创。比如有些博客参考了别人博客的部分代码,然后另外大部分是自己写的,那算不算原创,,,也罢,目的也只是记录一下,同时调整一下布局,弄的好看点,以便以后参考。
安卓手机中底下都会有三个按钮(魅族奇葩),菜单,返回,HOME键。
返回键用的最多,有独立的方法去监听:onBackPressed。
菜单键用的越来越少了,监听:onKeyDown或者onKeyUp都可。
但是HOME键并没有直接的方法去监听,这里使用了广播监听。
原文原话:在每次点击Home按键时都会发出一个action为Intent.ACTION_CLOSE_SYSTEM_DIALOGS的广播,它是关闭系统Dialog的广播,我们可以通过注册它来监听Home按键消息。
工具类
package com.example.qiao.test.utils;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
/**
* HOME键监听类。
*/
public class HomeKeyListener extends BroadcastReceiver {
private Context context;
public HomeKeyListener(Context context) {
this.context = context;
}
/**
* 通常在Activity的onStart方法中调用
*/
public void start() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.registerReceiver(this, filter);
}
/**
* 通常在Activity的onStop方法中调用
*/
public void stop() {
context.unregisterReceiver(this);
}
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)) {
String reason = intent.getStringExtra("reason");
if ("homekey".equals(reason)) {
// 按下HOME健
if (mOnHomeKeyPressListener != null) {
mOnHomeKeyPressListener.onHomeKeyPress();
}
} else if ("recentapps".equals(reason)) {
// 长按HOME键
if (mOnHomeKeyLongPressListener != null) {
mOnHomeKeyLongPressListener.onHomeKeyLongPress();
}
}
}
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 按下
// (这里把 Press 和 LongPress 分开是为了能够使用Lambda)
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private OnHomeKeyPressListener mOnHomeKeyPressListener;
public void setOnHomeKeyPressListener(OnHomeKeyPressListener listener) {
mOnHomeKeyPressListener = listener;
}
public interface OnHomeKeyPressListener {
void onHomeKeyPress();
}
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// 长按
// (长按通常不用。很多手机把长按做成了系统级别的其它功能,比如启动语音助手)
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
private OnHomeKeyLongPressListener mOnHomeKeyLongPressListener;
public void setOnHomekeyLongPressListener(OnHomeKeyLongPressListener listener) {
mOnHomeKeyLongPressListener = listener;
}
public interface OnHomeKeyLongPressListener {
void onHomeKeyLongPress();
}
}
使用例子
package com.example.qiao.test;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.Toast;
import com.example.qiao.test.utils.HomeKeyListener;
public class MainActivity extends Activity {
private Context context;
private HomeKeyListener listener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.context = this;
listener = new HomeKeyListener(this);
listener.setOnHomeKeyPressListener(() -> {
Toast.makeText(context, "按下了HOME键", Toast.LENGTH_SHORT).show();
});
listener.setOnHomekeyLongPressListener(() -> {
Toast.makeText(context, "长按了HOME键", Toast.LENGTH_SHORT).show();
});
}
@Override
protected void onStart() {
listener.start();
super.onStart();
}
@Override
protected void onStop() {
listener.stop();
super.onStop();
}
}