即使我的应用程序不在前面,如何接收Bixby按钮按下事件? [英] How to receive a Bixby button press event, even if my app is not in front?

查看:50
本文介绍了即使我的应用程序不在前面,如何接收Bixby按钮按下事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为Samsung Galaxy S8上的Bixby按钮(键代码:1082)编程,使其将启动我的应用程序而不是Bixby启动器?"App All in an Gestures" 已具有此功能.使用自定义键,但是我该如何在Android中以编程方式执行此操作?

How can I program the Bixby button (KeyCode: 1082) on the Samsung Galaxy S8, that it will start my application instead of the Bixby launcher? The App All in one Gestures has already this function with custom keys, but how can I do this in Android programatically?

推荐答案

三星似乎不赞成这样做的用户,并且显然在最近的更新中(至少在某些地方)禁用了此功能.报告各不相同,但请注意,下面的示例可能不适用于所有设备,或在不久的将来根本不适用.以下文章提供了更多详细信息(XDA开发人员的异地链接):

It seems Samsung doesn't approve of users doing this, and has apparently disabled this functionality in recent updates, at least in some places. Reports vary, but be warned that the example below might not work on every device, or at all in the near future. More details are available in the following article (off-site link to XDA Developers):

三星已删除了重新映射Galaxy S8/S8 +上的Bixby按钮的功能

一体式手势使用 AccessibilityService 来完成此任务.您的应用程序可以执行相同的操作,但是用户必须在设备设置中明确将您的应用程序作为可访问性服务启用.

All in one Gestures uses an AccessibilityService to accomplish this. Your app can do the same, but the user would have to explicitly enable your app as an Accessibility Service in the device Settings for it to work.

Bixby按钮显然会发出简单的 KeyEvent ,其键码为1082.您的 AccessibilityService 仅需要覆盖 onKeyEvent()方法,并检查传入的事件的键码.例如:

The Bixby button apparently emits simple KeyEvents with a keycode of 1082. Your AccessibilityService just needs to override the onKeyEvent() method, and check the keycode of the event passed in. For example:

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 ,以使其有资格被用户启用.例如,在< application> 标记之间:

You'll need to properly register your AccessibilityService in the manifest for it to be eligible to be enabled as such by the user. For example, between the <application> tags:

<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>

上面< meta-data> 元素上的 resource 属性指向具有 Service 必要设置的XML文件.在项目的 res/文件夹下,根据需要创建一个 xml/文件夹,然后在此文件夹中添加此文件:

The resource attribute on the <meta-data> element above points to an XML file with the necessary settings for the Service. Under your project's res/ folder, create an xml/ folder if needed, and add this file there:

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"
/>

安装后,您需要在设备设置的辅助功能"部分下的服务"中启用您的应用.

After installation, you will need to enable your app in the Services under the Accessibility section in the device Settings.

这篇关于即使我的应用程序不在前面,如何接收Bixby按钮按下事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆