我想添加一个监听器,用于监听长按Android系统后退按钮,即长按版本的
Activity.onBackPressed
这只需要在我的应用程序可见时起作用。
Chrome 在长按返回时显示历史菜单,所以它一定是可能的,但找不到它的钩子。谢谢
您可以尝试覆盖
onKeyLongPress
中的 Activity
方法。
@Override
public boolean onKeyLongPress(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
System.out.println("Back button long pressed");
return true;
}
return super.onKeyLongPress(keyCode, event);
}
你可以看看firefox是如何实现长按后退的。
他们使用 onKeyDown/onKeyUp 和 onKeyLongPress 来实现它。
两者都使用的原因是因为
Android N 和华为设备已损坏 onKeyLongPress 事件 后退按钮,所以我们自己实现长按行为
- 对于短按,我们取消onKeyUp中的回调
- 对于长按,正常的按键被标记为已取消,因此不会在其他地方处理(但Android仍然提供触觉 反馈),长按动作运行
以下是他们的实现。
// See onKeyDown for why this is necessary
private var backLongPressJob: Job? = null
private fun shouldUseCustomBackLongPress(): Boolean {
val isAndroidN =
Build.VERSION.SDK_INT == Build.VERSION_CODES.N || Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1
// Huawei devices seem to have problems with onKeyLongPress
// See https://github.com/mozilla-mobile/fenix/issues/13498
val isHuawei = Build.MANUFACTURER.equals("huawei", ignoreCase = true)
return isAndroidN || isHuawei
}
private fun handleBackLongPress(): Boolean {
supportFragmentManager.primaryNavigationFragment?.childFragmentManager?.fragments?.forEach {
if (it is OnBackLongPressedListener && it.onBackLongPressed()) {
return true
}
}
return false
}
final override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
// Inspired by https://searchfox.org/mozilla-esr68/source/mobile/android/base/java/org/mozilla/gecko/BrowserApp.java#584-613
// Android N and Huawei devices have broken onKeyLongPress events for the back button, so we
// instead implement the long press behavior ourselves
// - For short presses, we cancel the callback in onKeyUp
// - For long presses, the normal keypress is marked as cancelled, hence won't be handled elsewhere
// (but Android still provides the haptic feedback), and the long press action is run
if (shouldUseCustomBackLongPress() && keyCode == KeyEvent.KEYCODE_BACK) {
backLongPressJob = lifecycleScope.launch {
delay(ViewConfiguration.getLongPressTimeout().toLong())
handleBackLongPress()
}
}
return super.onKeyDown(keyCode, event)
}
final override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {
if (shouldUseCustomBackLongPress() && keyCode == KeyEvent.KEYCODE_BACK) {
backLongPressJob?.cancel()
}
return super.onKeyUp(keyCode, event)
}
final override fun onKeyLongPress(keyCode: Int, event: KeyEvent?): Boolean {
// onKeyLongPress is broken in Android N so we don't handle back button long presses here
// for N. The version check ensures we don't handle back button long presses twice.
if (!shouldUseCustomBackLongPress() && keyCode == KeyEvent.KEYCODE_BACK) {
return handleBackLongPress()
}
return super.onKeyLongPress(keyCode, event)
}
我希望下面的想法能够奏效!!!
在您的活动中添加以下代码
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//This event will be triggered after 1 second.
//In case if you move out of KEYCODE_BACK, onKeyUp event will be triggered and this event will be cancelled
handler.postDelayed(mLongPressed, 1000);
return true;
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
handler.removeCallbacks(mLongPressed);
return super.onKeyUp(keyCode, event);
}
处理程序代码:
final Handler handler = new Handler();
Runnable mLongPressed = new Runnable() {
public void run() {
Log.i("Activity", "Long press detected").
//Add your code
}
};
添加 chromium 项目的实现。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Detecting a long press of the back button via onLongPress is broken in Android N.
// To work around this, use a postDelayed, which is supported in all versions.
if (keyCode == KeyEvent.KEYCODE_BACK) {
mHandler.postDelayed(mShowHistoryRunnable, ViewConfiguration.getLongPressTimeout());
return super.onKeyDown(keyCode, event);
}
return super.onKeyDown(keyCode, event);
}
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mHandler.removeCallbacks(mShowHistoryRunnable);
}
return super.onKeyUp(keyCode, event);
}