java - 如何将 InputMethodService 与服务连接?
问题描述
我想将扩展 InputMethodService 的自定义软键盘与另一个服务连接。我需要从通用服务调用我的软键盘中的一个方法。
问题是我无法扩展 Binder 类,因为方法 onBind() 在 AbstractInputMethodService 中定义为 final ...
现在,如何从其他服务调用我的 SoftKeyboard 类中的方法?
解决方案
我已经通过使用广播接收器类解决了这个问题。
class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("GET_KEY_KEY")) {
String msg = intent.getStringExtra("msg");
InputConnection ic = getCurrentInputConnection();
if (ic != null) {
ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_RIGHT));
}
}
}
}
然后我向我的服务发送了一个广播。
Intent intent = new Intent();
intent.setAction("GET_KEY_KEY");
sendBroadcast(intent);
推荐阅读
- java - JDA:一旦用户被封禁,机器人就不会通知用户
- qt - 在 Qt 6 中用 QVideoWidget 替换 QCameraViewfinder
- ruby-on-rails - Array#product RangeError:产品太大
- doxygen - 我遇到了`cmake ..`的问题
- pandas - 名称未在 Python for 循环中定义
- mysql - 如何将 API 分成不同的文件?(路线)Node.js + MySQL
- laravel - 如何在路由资源(laravel)上的一个功能上使用中间件
- cobol - 在 COBOL 中使用压缩十进制进行数组处理和表处理
- visual-studio-code - 如何删除 VS Code 终端中的“&”符号?
- hmac - 我使用 https 请求我的授权 api 并且消息显示未经授权