Question

CLI version 3.2.3, Titanium SDK version 3.2.2.GA

I am using a external jar file and trying to make it work into the native module so that native module can be used with titanium

i am not able to fire an event from the handler

i have two classes in native module clsModule and 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 ?

    }
}

i tried with fireEvent but that did not work it gives a error that it is not defined.

Was it helpful?

Solution

Your module (as do all modules) extends KrollModule which extends KrollProxy, which handles events. Your Handler class does not have access to this object so it cannot fire the event itself, however you are already passing that reference (new Handler(this)) so just use it!

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

This is the general idea, pass the proxy to fire an event to the class itself, an alternative would be to inline an anonymous implementation of ResponseHandler inside your module instead. I'm not sure if you are implementing ResponseHandler right, or if OnConnected is even firing, you need to check that first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top