我要为长按android系统后退按钮(即长按版本ProtoObject
这仅在我的应用可见时才有效。
当长按后退按钮时,Chrome会显示历史记录菜单,因此必须可以,但找不到对应的钩子。谢谢
答案 0 :(得分:1)
您可以尝试覆盖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);
}
答案 1 :(得分:1)
您可以查看firefox如何实现Long Back Press。
他们使用onKeyDown / onKeyUp和onKeyLongPress来实现它。
两者都被使用的原因是
Android N和华为设备已中断了onKeyLongPress事件, 后退按钮,因此我们可以自己实现长按行为
- 对于短按,我们取消了onKeyUp中的回调
- 对于长按,正常的keyPress被标记为已取消,因此不会在其他地方处理(但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)
}
答案 2 :(得分:0)
我希望下面的想法可行!
在活动中添加以下代码
@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
}
};