How to send events from Java to ActionScript

Opt In Image
Early bird offer on the ANE eBooks

 

Buy any Easy Native Extensions 2nd Edition package and get our $99 iOS + Android ANE Template completely free before the end of June 2015.

 

 

  • step-by-step guide to making your iOS extension in under an hour
  • library for data conversion between ActionScript and native code
  • tutorials
  • infographics
  • code included

When we looked at how native code and ActionScript communicate in a native extension, we saw that native code is mostly called and talked to and has a limited number of ways to respond or send data back. These are:

  1. Returning a FREResult object from a call to FREFunction.
  2. Using output parameters in a FREFunction.
  3. Sending an event to ActionScript.

In this article we are focusing on number 3: how events are sent from Java to ActionScript.

Events from Java to ActionScript are asynchronous

We already saw why in the post about events from C to ActionScript, but I’ll repeat it here, as it’s important:

  • there isn’t a synchronous way for native code to call an ActionScript function via the AIR Java API the way that ActionScript makes direct calls to native code. When information needs to be sent to ActionScript outside a call that ActionScript initiated, your option is to send an event that ActionScript will receive asynchronously.
  • you can send an event to ActionScript form any thread in your native code and it’s delivered on AIR’s single main thread. This is the only AIR Java API call you are allowed to make outside the main thread.

FREContext.dispatchStatusEventAsync

In Java dispatchStatusEventAsync() is a method of the FREContext class, which represents your Extension Context.

  • code is a string, which you set. Use it to identify the event you are sending, for example “SPEECH_RECOGNITION_EVENT”.
  • level is another string that you set. Use this to send more detailed information about the event to ActionScript, for example “Speech recognition results” or “No results found”.

How this is received by ActionScript

On the ActionScript side the target of the event is the instance of flash.externalExtensionContext, which was passed to FREDispatchStatusEventAsync. Wondering where it comes from? Read the next section.

The event’s type is flash.events.StatusEvent. You’ll notice that, among other things, StatusEvent has the following two properties:

  • code : String
  • level : String

See how this maps to the information sent from the Java side?

An example

A call to dispatchStatusEventAsynch() from your Java code:

will be received in your ActionScript code in the form of a StatusEvent in a StatusEvent.STATUS handler that you provide:

This will print out:

What else?

Here’s a definition of Extension Context, if you need a reminder.

Have a look at how events are sent from C to ActionScript.

Next we’ll see what are the Extension Context Initializer and Finalizer.

Then we will see the order of calls for all these initializers and finalizers.

Over to you

Why do you code in Java? No, seriously.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/easyna6/public_html/easynativeextensions_wp/wp-includes/class-wp-comment-query.php on line 399

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">