Android 以编程方式调用硬件后退按钮?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11747420/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call the hardware back button programmatically?
提问by JuiCe
I know that one can overwrite the Back Button functionality in Android, but I was wondering if there was a method or anything I could call that would functionally do the same thing as pressing the hardware button.
我知道可以覆盖 Android 中的后退按钮功能,但我想知道是否有一种方法或任何我可以调用的方法可以在功能上执行与按下硬件按钮相同的操作。
回答by Frank Sposaro
You can send the back button press to the system like this
您可以像这样将后退按钮按下发送到系统
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
Or you can override the back button press and call
或者您可以覆盖后退按钮按下并调用
finish();
on your Activity. That basically does the same thing as the generic back button.
在您的活动中。这基本上与通用后退按钮的作用相同。
回答by hoi
Just call this.onBackPressed();
in Activity.
只需this.onBackPressed();
在活动中调用。
回答by Evgenii Vorobei
If you want just "press" hardware button,
create service extended from AccessibilityService
:
如果您只想“按下”硬件按钮,请创建从AccessibilityService
以下扩展的服务:
class ExampleAccessService:AccessibilityService() {
override fun onInterrupt() {
}
override fun onAccessibilityEvent(event: AccessibilityEvent?) {
}
fun doAction(){
performGlobalAction(GLOBAL_ACTION_RECENTS)
// performGlobalAction(GLOBAL_ACTION_BACK)
// performGlobalAction(GLOBAL_ACTION_HOME)
// performGlobalAction(GLOBAL_ACTION_NOTIFICATIONS)
// performGlobalAction(GLOBAL_ACTION_POWER_DIALOG)
// performGlobalAction(GLOBAL_ACTION_QUICK_SETTINGS)
// performGlobalAction(GLOBAL_ACTION_TOGGLE_SPLIT_SCREEN)
}
}
Call doAction()
where you want action
呼叫doAction()
您想要采取行动的地方
Add to Manifest
:
添加到Manifest
:
<application
...
<service
android:name=".ExampleAccessService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="Name of servise" // it will be viewed in Settings->Accessibility->Services
android:enabled="true"
android:exported="false" >
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/accessibility_service_config"/>
</service>
...
</application>
accessibility_service_config.xml:
可访问性_service_config.xml:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackAllMask"
android:accessibilityFlags="flagDefault"
android:canRetrieveWindowContent="false"
android:description="your description"
android:notificationTimeout="100"
android:packageNames="your app package, ex: ex: com.example.android"
android:settingsActivity="your settings activity ex: com.example.android.MainActivity" />
for more info look at https://developer.android.com/guide/topics/ui/accessibility/services.html
有关更多信息,请查看https://developer.android.com/guide/topics/ui/accessibility/services.html