androidactionscript-3event-handlingflex4.6air-native-extension

How to listen event dispatched from ANE?


What the title says.

I have in my native code :

freContext.dispatchStatusEventAsync("myEvent","");

in the as3 part of the ane :

 private static function onStatus( event:StatusEvent ):void {
        if(event.code == MyEvent.EVENT_NAME){ // EVENT_NAME = "myEvent"
            displayToast("dispatching event");
            dispatcher.dispatchEvent(MyEvent(MyEvent.EVENT_NAME));
        }
    }

and in my flex application (MyAppHomeView.mxml)

<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark" 
        xmlns:mx="library://ns.adobe.com/flex/mx"
        title="Home View"
        initialize="createListener()">

    <fx:Script>
        <![CDATA[


            protected function createListener():void{
                MyANE.showDialogMessage("init");
                addEventListener(MyEvent.EVENT_NAME,handleEvent);
            }

            protected function handleEvent(event:MyEvent):void{
                MyANE.showDialogMessage("myEvent");
            }



        ]]>
    </fx:Script>


</s:View>

I do get the "dispatching event" toast (which means the native code part works fine), but not the "myEvent" dialog. What I'm doing wrong?


Solution

  • Thanks to Organis comment and adobe's example here : https://github.com/nweber/SystemVolumeNativeExtension/blob/master/VolumeTest/src/VolumeTest.mxml I managed to fix my problem :

    First MyAne class must extends EventDispatcher. Then in onStatus :

    private function onStatus( event:StatusEvent ):void {
        if(event.code == MyEvent.EVENT_NAME){ // EVENT_NAME = "myEvent"
            displayToast("dispatching event");
            dispatchEvent(new MyEvent(MyEvent.EVENT_NAME));
        }
    }
    

    and in the mxml, create an instance of MyANE and call addEventListener on it.

    <fx:Script>
            <![CDATA[
    
    
                var ane:MyANE = new MyANE();
    
                protected function createListener():void{
                    MyANE.showDialogMessage("init");
                    ane.addEventListener(MyEvent.EVENT_NAME,handleEvent);
                }
    
                protected function handleEvent(event:MyEvent):void{
                    MyANE.showDialogMessage("myEvent");
                }
    
    
    
            ]]>
    </fx:Script>