Dispatching and handling an event from an ANE (Air Native Extension) to a flex mobile application -


the explanation of problem requires background info making question little long, bear me.

an ane (air native extension) consists of 3 parts, native code allows use device specific features (i'm using android), actionscript library accesses native code functions , makes them available flex mobile application, , flex mobile application.

for ane, i'm getting internet info in onreceive() method extend broadcast receiver. method doesn't return anything, when it's done store in global variable , dispatch event saying info ready.

the actionscript interface listens event , when receives it, updates global variable , dispatches event telling flex mobile app info ready.

so when flex mobile app learns info needs ready goes in , gets it.

here's example of how coded if explanation wasn't clear enough:

native code: public myclass(anotherclass v,frecontext c){     this.v = v;     this.c = c;       //will use c dispatch event } onreceive(){     ....code....     string x = "internet info"     anotherclass.setglobalvar(x);     c.dispatchstatuseventasync("status", "internetinfoready"); }   actionscript interface code: private var context:extensioncontext; var info:string; public function interface(){     context = extensioncontext.createextensioncontext("id",null);     context.addeventlistener(statusevent.status,onstatus); }  public function scan():void{     //calls function runs asynchronously internet info } public function onstatus(event:statusevent):void{     if((event.level == "status") && (event.code="internetinfoready")){         info=string(context.all("getinfo"));  //function retrieves value x in anotherclass , returns         dispatchevent(new event("internetinfoready"));     }  }  public function getinfo():string{     return info; }   flex mobile app code: <s:view .... <fx:script>     <![cdata[         var a:interface = new interface();          protected function getinfo(event:mouseevent):void{             a.scan();             test.addeventlistener("internetinfoready",ongetinfo);         }          protected function ongetinfo(evt:event):void{             var info:string = "";             if(evt.type == "internetinfoready"){                 info = a.getinfo();                 a.toast(info);  //function calls android built in toast commands             }         }     ]]> </fx:script>  <fx:declarations>     <!-- place non-visual elements (e.g., services, value objects) here --> </fx:declarations> <s:button left="251" right="250" top="40" height="43" label="get info" click="getinfo(event)"           fontfamily="arial" horizontalcenter="0"/> 

the app isn't running on device , want make sure i'm dispatching events right. created android app uses native code written display information gotten on screen can make sure part works, , know that's not problem.

your as3 interface class must extends eventdispatcher.

notices 1 strange thing in case custom event in extension project

dispatchevent(new customevent(customevent.constant, somedata)); 

does not fire event, but

var ce:customevent = new customevent(customevent.constant);     ce.data = somedata;     dispatchevent(ce); 

works fine


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -