如何解决无障碍服务未显示 - Android -cordova?
我正在开发一个 Cordova 插件,该插件使用 AccessibilityService
来记录按键并使用作为按键的参数执行回调。我正在使用此服务 here 中的代码,但稍有改动。
我遇到的问题是辅助功能服务没有出现在系统设置中。现在,我刚刚在 android manifest 中添加了 XML。
我正在使用以下代码:
插件.xml
<?xml version="1.0" encoding="UTF-8"?>
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-plugin-keylogger"
version="1.0.0">
<name>KeyLogger</name>
<description>Cordova Keylogger Plugin</description>
<license>Apache 2.0</license>
<keywords>cordova,keyevents</keywords>
<js-module src="www/keylogger.js" name="device">
<clobbers target="Keylogger" />
</js-module>
<!-- android -->
<platform name="android">
<config-file target="res/xml/config.xml" parent="/*">
<feature name="keylogger" >
<param name="android-package" value="org.apache.cordova.keylogger"/>
</feature>
</config-file>
<source-file src="src/android/KeyLogger.java" target-dir="src/org/apache/cordova/keylogger" />
<source-file src="src/android/KeyCallBack.java" target-dir="src/org/apache/cordova/keylogger" />
<source-file src="src/android/KeyLoggerAccessibilityService.java" target-dir="src/org/apache/cordova/keylogger" />
</platform>
</plugin>
keylogger.js
(function () {
var exec = require('cordova/exec');
var channel = require('cordova/channel');
var modulemapper = require('cordova/modulemapper');
var urlutil = require('cordova/urlutil');
function KeyLogger() {
this.channels = {
keypress: channel.create('keypress'),};
}
KeyLogger.prototype = {
_eventHandler: function (event) {
if (event && event.type in this.channels) {
this.channels[event.type].fire(event);
}
},stop: function (eventname) {
exec(null,null,'KeyLogger','stop',[]);
},addEventListener: function (eventname,f) {
if (eventname in this.channels) {
this.channels[eventname].subscribe(f);
}
},removeEventListener: function (eventname,f) {
if (eventname in this.channels) {
this.channels[eventname].unsubscribe(f);
}
},};
module.exports = function (callback) {
const kl = new KeyLogger();
kl.addEventListener('keypress',callback);
var cb = function (eventname) {
kl._eventHandler(eventname);
};
exec(cb,cb,'start',[]);
return kl;
};
})();
KeyLogger.java
package org.apache.cordova.keylogger;
import org.apache.cordova.PluginResult;
import org.apache.cordova.CordovaArgs;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
public class KeyLogger extends CordovaPlugin {
private CallbackContext callbackContext;
private KeyCallBack callback;
private KeyLoggerAccessibilityService accessibilityservice;
public boolean execute(String action,CordovaArgs args,final CallbackContext callbackContext)
throws JSONException {
if (action == "start") {
this.callbackContext = callbackContext;
this.callback.set(callbackContext);
this.accessibilityservice.setKeyCallBack(this.callback);
} else if (action == "stop") {
this.accessibilityservice.disableSelf();
} else {
return false;
}
return true;
}
}
KeyLoggerAccessibilityService.java
package org.apache.cordova.keylogger;
import android.accessibilityservice.AccessibilityService;
import android.view.accessibility.AccessibilityEvent;
import android.accessibilityservice.AccessibilityServiceInfo;
public class KeyLoggerAccessibilityService extends AccessibilityService {
KeyCallBack callback;
@Override
public void onAccessibilityEvent(AccessibilityEvent event) {
String text = "";
text = text + event.getText();
this.callback.send(text);
}
@Override
public void onInterrupt() {
// whatever
}
@Override
public void onServiceConnected() {
// configure our Accessibility service
AccessibilityServiceInfo info = getServiceInfo();
info.eventTypes = AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED;
info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
info.notificationTimeout = 100;
this.setServiceInfo(info);
}
void setKeyCallBack(KeyCallBack cb) {
this.callback = cb;
}
}
KeyCallback.java
package org.apache.cordova.keylogger;
import org.apache.cordova.PluginResult;
import org.apache.cordova.LOG;
import org.json.JSONException;
import org.json.JSONObject;
import org.apache.cordova.CallbackContext;
public class KeyCallBack {
private CallbackContext callbackContext;
public void set(CallbackContext cc) {
this.callbackContext = cc;
}
public void send(String s) {
try {
JSONObject obj = new JSONObject();
obj.put("type","keypress");
obj.put("keypress",s);
this.sendUpdate(obj);
} catch (JSONException ex) {
LOG.d("KeyLogger","Should never happen");
}
}
private void sendUpdate(JSONObject obj) {
sendUpdate(obj,true,PluginResult.Status.OK);
}
private void sendUpdate(JSONObject obj,boolean keepCallback,PluginResult.Status status) {
if (callbackContext != null) {
PluginResult result = new PluginResult(status,obj);
result.setKeepCallback(keepCallback);
callbackContext.sendPluginResult(result);
if (!keepCallback) {
callbackContext = null;
}
}
}
}
大部分代码是从 InAppBrowser 插件中复制而来,然后进行修改。
如果我的方法不正确,请指导我正确的方向。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。