【问题标题】:how to fire event from native module如何从本机模块触发事件
【发布时间】:2014-05-04 14:54:57
【问题描述】:

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

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

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

我在本机模块中有两个类 clsModule 和 Handler

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);
    }
}

Handler.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,但没有成功,它给出了一个未定义的错误。

【问题讨论】:

  • 还在寻找解决方案?
  • 你尝试过 Ti.App.fireEvent 吗?

标签: java android titanium titanium-modules


【解决方案1】:

您的模块(与所有模块一样)扩展了 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 是否正在触发,您需要先检查一下。

【讨论】:

    猜你喜欢
    • 2018-06-14
    • 2014-12-07
    • 1970-01-01
    • 1970-01-01
    • 2012-09-09
    • 2018-11-25
    • 1970-01-01
    • 1970-01-01
    • 2015-11-18
    相关资源
    最近更新 更多