点击home键会接收一个广播:
Intent.ACTION_CLOSE_SYSTEM_DIALOGS
则我们注册接收这个广播即可
private void home() {
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
HomeReceiver homeReceiver = new HomeReceiver();
registerReceiver(homeReceiver, intentFilter);
}
class HomeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra("reason");
if (reason != null) {
if (reason.equals("homekey")) {
//点击了home键
Toast.makeText(context, "home", Toast.LENGTH_SHORT).show();
} else if (reason.equals("recentapps")) {
//长按home键,显示最近的应用菜单
Toast.makeText(context, "recentapps", Toast.LENGTH_SHORT).show();
}
}
}
}
}