Create an Air ANE for Android to dispatch background tasks


If you are using DIRECT or GPU mode with Adobe Air you probably have already noticed that once the app goes in background it stops firing any kind of Event. This means that any task that dispatches Event will stop working.

Luckily it is still possible to dispatch Events from an ANE.

In this post I am going to create an ANE for Android that dispatches an Event every second which triggers a callback on the AS3 side.

Assuming that you have already downloaded the Android SDK, open Eclipse and create a new Android Application Project naming it BackgroundTasker, also if you want change the Package Name, I changeds mine to com.lorenzonuvoletta.backgroundtasker.

Click Next a bunch of times and you should have your project setup.

You can also go ahead and delete the file MainActivity.java in src folder, you don’t need that.

Also from your Air SDK folder copy the file “\lib\android\FlashRuntimeExtensions.jar” into your “BackgroundTasker\lib\” folder and add it to your project by, Richt Click on porject name -> Properties -> Java Build Path -> Add External JARs…

 

It’s time to create the first Class, right click on the package in “src” and select Class, name it OneSecondTask and use FREFunction as interface.

This class will simply dispatch an Event every second, here is the code for it

Create a new Class and name it BackgroundTaskerContext, and choose as Superclass FREContext, this class will map to the previously created function, this is the code for it:

And finally create the class that handles the context, make sure to use FREExtension as the interface, this is the code:

At this point you should not have any error, export the JAR file by clicking on File -> Export -> Java -> JAR file, and when it asks to select the resources, simply select the “src” folder.

Also create an XML file that looks like this:

Almost there, the last piece is the ActionScript class that will glue the Extension, in this class you start the tasker by passing a callback method which is going to be called every second by the ANE, this is the code for it:

Ok now it’s time to generate the ANE and SWC files.

Open terminal and write:

  •  set PATH_FLEXSDK=”C:\Program Files (x86)\FlashDevelop\Tools\flexsdk” (Use whatever path you have for the Flex SDK, make sure it also contains the Air SDK)
  • %PATH_FLEXSDK%\bin\compc -source-path as -include-classes com.lorenzonuvoletta.backgroundtasker.BackgroundTasker -swf-version 20 -external-library-path %PATH_FLEXSDK%\frameworks\libs\air\airglobal.swc -output com.lorenzonuvoletta.backgroundtasker.BackgroundTasker.swc
This will generate a SWC file, open the SWC like it was a ZIP file and extract the file “library.swf” and put that in the same location where you saved your JAR file.
In my case the folder is named “jar”.
  • %PATH_FLEXSDK%\bin\adt -package -target ane com.lorenzonuvoletta.backgroundtasker.BackgroundTasker.ane extension.xml -swc com.lorenzonuvoletta.backgroundtasker.BackgroundTasker.swc -platform Android-ARM -C jar .
This will create an ANE.
Now that you have the SWC and the ANE, just add them to the “lib” folder of your project.
Usage example:

When you compile make sure you add this to your application XML:

Also if you are using FlashDevelop make sure you add “-extdir lib” in the Package.bat where it calls adt.

If you are interested on how to create an iOS ANE Extension, look at this other post.


13 thoughts on “Create an Air ANE for Android to dispatch background tasks

  • Kalle

    Awesome! Thanks for this post!
    Do you happen to have a working solution for iOS also?
    I’m using stage3D in an app and need to save user’s GPS position in the background.

    Regards from Sweden,

    Kalle

  • mikeciz

    This is great! I got it working perfectly. Is there a way to make the background task persistent? In other words, android won’t go and shut it down while it’s running. Here’s an example.

    For example, if I launch the camera application it will sometimes shut down my AIR application sometimes. When I return from taking a picture my app is closed sometimes and data is lost. I’m thinking that if the background task was persistent it wouldn’t shut down the AIR application.

      • Tomer

        I create an app that works on the cpu but when it goes in the background it reduces the framerate which is fine by me. After several minutes around 30 minutes or a little more the application is getting shut down automaticcaly.
        Maybe do you know how it’s possible to make it active for hours untill the user will kill the app?

  • Anas

    Hi lorenzo.
    is there any rule of setting the extension version number ?? I can see you are using 3.7. How do i know which version number is correct for me ? is that the AIR SDK version number ?

  • Fred

    Hi,
    I’ve been working with generate SWC. Spend a lot of time to get simply nothing. :)
    Could U help me with this command:

    %PATH_FLEXSDK%\bin\compc -source-path c:\ANE\pl\test\backgroundtasker\BackgroundTasker.as -include-classes pl.test.backgroundtasker -swf-version 20 -external-library-path %PATH_FLEXSDK%\frameworks\libs\air\airglobal.swc -output pl.test.backgroundtasker.BackgroundTasker.swc

    ERROR:
    file name or directory is incorrect.
    Everything’s fine (files name & location) , i’ve checked many many times. What I doin wrong?

  • Mehdi Mahjourian

    Hi
    I read your post about “Create an Air ANE for Android to dispatch background tasks”
    but i am rookie at eclips ! Is there video tutorial version of your post on the web ?
    thanks

  • Arvind

    Hi Lorenzo,

    I am using same steps, ANE created fine but while I am using in Adobe AIR code then its returns null. Please help me out.

    Warm regards,
    Arvind

  • Wildflash

    I was stuck also with getting null context after following each steps described by Lorenzo, my fix was:

    Right click the native project on Eclipse
    Select “Java Compiler” on left side, then tick the “Enable project specific settings”
    Change “Compiler compliance level” to use 1.6. My default setting was 1.7, turn out that creating ANE still compatible with java version 1.6, I believe I have read it somewhere on the net.

    Hope it helps!

    Thanks for your nice tutorial Lorenzo.

Leave a comment

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