我正在开发一个使用 AccessibilityService
记录按键并执行参数为按键的回调。我在这里使用这个服务的代码,只是做了一些小改动。我遇到的问题是辅助功能服务没有显示在系统设置中。现在,我刚刚在android清单中添加了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>
键盘记录器.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, 'KeyLogger', 'start', []);
return kl;
};
})();
键盘记录器.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插件复制的,然后修改。
如果我的方法不正确,请指导我正确的方向。
暂无答案!
目前还没有任何答案,快来回答吧!