You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无root权限下Android应用使用INJECT发送按键事件遇权限问题求助

无Root权限下给Google TV模拟按键事件的可行方案

方案1:利用AccessibilityService(辅助功能服务)

这是普通应用实现模拟输入的官方合法途径,无需Root,仅需用户在系统设置中启用对应辅助功能。

步骤:

  1. 创建AccessibilityService子类,实现按键发送逻辑:
public class RemoteControlAccessibilityService extends AccessibilityService {
    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {}

    @Override
    public void onInterrupt() {}

    // 发送自定义按键事件(比如方向键、确认键)
    public boolean sendKeyEvent(int keyCode) {
        KeyEvent downEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
        KeyEvent upEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
        boolean downSuccess = dispatchKeyEvent(downEvent);
        boolean upSuccess = dispatchKeyEvent(upEvent);
        return downSuccess && upSuccess;
    }

    // 发送系统预设全局按键(比如主页、返回)
    public boolean sendGlobalAction(int action) {
        return performGlobalAction(action);
    }
}
  1. 在res/xml目录下创建辅助服务配置文件accessibility_service_config.xml:
<accessibility-service xmlns:android="http://schemas.android.com/apk/res/android"
    android:accessibilityEventTypes="typeAllMask"
    android:accessibilityFeedbackType="feedbackGeneric"
    android:accessibilityFlags="flagDefault"
    android:description="@string/accessibility_service_desc" />
  1. 在AndroidManifest.xml中注册服务:
<service
    android:name=".RemoteControlAccessibilityService"
    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/accessibility_service_config" />
</service>
  1. 引导用户跳转到辅助功能设置页面启用服务:
Intent intent = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
startActivity(intent);

启用后,即可通过sendKeyEvent方法发送KeyEvent.KEYCODE_DPAD_UP、KeyEvent.KEYCODE_ENTER等按键事件到Google TV。

方案2:模拟蓝牙HID键盘

如果你的机器人设备支持蓝牙,可将应用模拟为蓝牙键盘,通过HID协议向Google TV发送按键事件,无需Root,仅需完成蓝牙配对。

步骤:

  1. 在Manifest中添加蓝牙权限:
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- Android 10+ 必需 -->
  1. 利用BluetoothHidDevice实现按键发送:
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();

bluetoothAdapter.getProfileProxy(this, new BluetoothProfile.ServiceListener() {
    @Override
    public void onServiceConnected(int profile, BluetoothProfile proxy) {
        if (profile == BluetoothProfile.HID_DEVICE) {
            BluetoothHidDevice hidDevice = (BluetoothHidDevice) proxy;
            // 配对Google TV后,构造按键报告发送
            byte[] report = new byte[8];
            report[2] = (byte) KeyEvent.KEYCODE_DPAD_RIGHT; // 对应目标按键码
            hidDevice.sendReport(pairedDevice, BluetoothHidDevice.REPORT_TYPE_INPUT, report);
        }
    }

    @Override
    public void onServiceDisconnected(int profile) {}
}, BluetoothProfile.HID_DEVICE);

注意:需处理蓝牙配对流程,不同Android版本的蓝牙API存在差异,需针对性适配。

方案3:使用ADB Shell命令(需用户开启USB调试)

若用户可在Google TV上开启USB调试,调试版应用可通过执行input keyevent命令发送按键事件:

try {
    String keyCode = "21"; // 对应KEYCODE_DPAD_LEFT
    Process process = Runtime.getRuntime().exec("input keyevent " + keyCode);
    process.waitFor();
} catch (IOException | InterruptedException e) {
    e.printStackTrace();
}

该方案仅适用于调试场景,正式发布的应用无法使用,且依赖用户手动开启USB调试。

内容的提问来源于stack exchange,提问作者TRESOR MALO

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 05:03:26