Android 最近的设备按钮侦听器
Android recent device button Listener
如何为最近的应用 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);
}
如果您指的是 "squared" 按钮,则不能。只能拦截 onBackPressed 事件。单击主页(圆圈)或最近使用的应用程序(方形)不会触发除 onPause 和 onStop 之外的任何其他事件。根据 Activity lifecycle.
是的,您可以使用 Action_Close_System_Dialog
获取最近的按钮回调
只需使用这个 class 一个单独的 class 名称 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();
}
}
call the function from the class you need it
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();
}
如何为最近的应用 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);
}
如果您指的是 "squared" 按钮,则不能。只能拦截 onBackPressed 事件。单击主页(圆圈)或最近使用的应用程序(方形)不会触发除 onPause 和 onStop 之外的任何其他事件。根据 Activity lifecycle.
是的,您可以使用 Action_Close_System_Dialog
获取最近的按钮回调只需使用这个 class 一个单独的 class 名称 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();
}
}
call the function from the class you need it
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();
}