如何为最近的Apps Android设备按钮获取回叫侦听器,就像按“后退按钮”一样。我尝试了onKeyPress
个事件,但是没有用。可以轻松找到“后退”按钮单击,但是如何检测最近的按钮单击?
我尝试了以下代码和许多其他代码:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_A:
{
//your Action code
return true;
}
}
return super.onKeyDown(keyCode, event);
}
答案 0 :(得分:1)
是的,您可以使用 Action_Close_System_Dialog
来获取最近的按钮回调只需在该类中使用单独的类名称Home-watcher
public class HomeWatcher
{
static final String TAG = "hg";
private Context mContext;
private OnHomePressedListener mListener;
private InnerReceiver mReceiver;
public HomeWatcher(Context context) {
mContext = context;
}
public void setOnHomePressedListener(OnHomePressedListener listener) {
mListener = listener;
mReceiver = new InnerReceiver();
}
public void startWatch() {
if (mReceiver != null) {
mContext.registerReceiver(mReceiver, new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
}
public void stopWatch() {
if (mReceiver != null) {
mContext.unregisterReceiver(mReceiver);
}
}
private class InnerReceiver extends BroadcastReceiver
{
final String SYSTEM_DIALOG_REASON_KEY = "reason";
final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
final String SYSTEM_DIALOG_REASON_RECENT_APPS = "recentapps";
final String SYSTEM_DIALOG_REASON_HOME_KEY = "homekey";
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(Intent.ACTION_CLOSE_SYSTEM_DIALOGS)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (reason != null) {
Log.e(TAG, "action:" + action + ",reason:" + reason);
if (mListener != null) {
if (reason.equals(SYSTEM_DIALOG_REASON_HOME_KEY)) {
mListener.onHomePressed();
} else if (reason.equals(SYSTEM_DIALOG_REASON_RECENT_APPS)) {
mListener.onHomeLongPressed();
}
}
}
}
}
@Override
public String toString() {
return getClass().getSimpleName() + " " + super.toString();
}
}
public interface OnHomePressedListener {
void onHomePressed();
void onHomeLongPressed();
}
}
从需要的类中调用函数
mHomeWatcher = new HomeWatcher(this);
mHomeWatcher.setOnHomePressedListener(new HomeWatcher.OnHomePressedListener() {
@Override
public void onHomePressed() {
// do something here...
Log.e("AHSAN", "onHomePressed: ");
proceed();
}
@Override
public void onHomeLongPressed() {
Log.e("AHSAN", "onHomeLongPressed: ");
proceed();
}
答案 1 :(得分:0)
如果您指的是“平方”按钮,则不能。仅onBackPressed事件可以被拦截。单击主页(圆圈)或最近使用的应用程序(正方形)将不会触发除onPause和onStop之外的任何其他事件。根据{{3}}。