如何对三星 Galaxy S8 上的 Bixby 按钮(KeyCode:1082)进行编程,使其启动我的应用程序而不是 Bixby 启动器? App All in one Gestures已经使用自定义键实现了此功能,但我如何在 Android 中以编程方式执行此操作?
最佳答案
三星似乎不赞成用户这样做,并且显然在最近的更新中禁用了此功能,至少在某些地方是这样。报告各不相同,但请注意,以下示例可能无法在所有设备上运行,或者在不久的将来根本无法运行。以下文章(指向 XDA Developers 的站外链接)中提供了更多详细信息:
Samsung has Removed the Ability to Remap the Bixby Button on the Galaxy S8/S8+
多合一手势使用 AccessibilityService
来完成这个。您的应用可以执行相同的操作,但用户必须在设备设置中明确启用您的应用作为辅助功能服务才能运行。
Bixby 按钮显然发出简单的 KeyEvent
s 的键码为 1082。您的 AccessibilityService
只需要覆盖 onKeyEvent()
方法,并检查传入的事件的键码。例如:
public class BixbyInterceptService extends AccessibilityService {
private static final int KEYCODE_BIXBY = 1082;
@Override
protected boolean onKeyEvent(KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BIXBY &&
event.getAction() == KeyEvent.ACTION_DOWN) {
// Do your thing here; startActivity(), Toast, Notification, etc.
Toast.makeText(this, "Bixby button pressed", 0).show();
// Return true to stop the event from propagating further.
return true;
}
return super.onKeyEvent(event);
}
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {}
@Override
public void onInterrupt() {}
}
您需要正确注册您的 AccessibilityService
在 list 中,它有资格被用户启用。例如,<application>
之间标签:
<service
android:name=".BixbyInterceptService"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE">
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService" />
</intent-filter>
<meta-data
android:name="android.accessibilityservice"
android:resource="@xml/bixby_service_config" />
</service>
resource
<meta-data>
上的属性上面的元素指向一个 XML 文件,其中包含 Service
的必要设置。 .在你项目的res/
下文件夹,创建一个 xml/
文件夹,如果需要,并在其中添加此文件:
bixby_service_config.xml
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityFlags="flagRequestFilterKeyEvents"
android:canRequestFilterKeyEvents="true"
/>
安装后,您需要在设备设置的辅助功能部分下的服务中启用您的应用。
关于android - 如何接收 Bixby 按钮按下事件,即使我的应用程序不在前面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43299480/