gpt4 book ai didi

java - 如何从 native 模块触发事件

转载 作者:行者123 更新时间:2023-11-30 02:54:50 25 4
gpt4 key购买 nike

CLI 版本 3.2.3,Titanium SDK 版本 3.2.2.GA

我正在使用一个外部 jar 文件并试图让它在 native 模块中工作,以便 native 模块可以与 titanium 一起使用

我无法从处理程序中触发一个事件

我在 native 模块中有两个类clsModule 和处理程序

clsModule.java

import ext.Extclient;

@Kroll.module(name="extclient", id="ti.extclient")
public class clsModule extends KrollModule{

private Handler h ;
Extclient ext ;

public clsModule()
{
super();
ext = new Extclient();
h = new Handler(this);
im.addHandler(h);
}
}

处理程序.java

import ext.ResponseHandler;

public class Handler implements ResponseHandler
{

public void OnConnected(String arg0, String arg1) {
HashMap<String, Object> event = new HashMap<String, Object>();
event.put("u1", arg0);
event.put("u2", arg1);

//How do i fire a event here ?

}
}

我尝试使用 fireEvent,但没有用,它给出了一个未定义的错误。

最佳答案

你的模块(和所有模块一样)扩展了 KrollModule延伸 KrollProxy ,它处理事件。您的 Handler 类无权访问此对象,因此它无法自行触发事件,但您已经传递了该引用 (new Handler(this)),所以只需使用它即可!

import ext.ResponseHandler;

public class Handler implements ResponseHandler {
private KrollModule proxy; // Hold onto this reference
public Handler(KrollModule proxy) {
this.proxy = proxy;
}

public void OnConnected(String arg0, String arg1) {
// Fire event if anyone is listening
if (proxy.hasListeners("colorChange")) {
HashMap<String, Object> event = new HashMap<String, Object>();
event.put("u1", arg0);
event.put("u2", arg1);
proxy.fireEvent("colorChange", hm);
}
}
}

这是一般的想法,将代理传递给类本身以触发事件,另一种方法是在模块中内联 ResponseHandler 的匿名实现。我不确定您是否正确实现了 ResponseHandler,或者 OnConnected 是否正在触发,您需要先检查一下。

关于java - 如何从 native 模块触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23457649/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com