androidactionscript-3apache-flexairair-native-extension

How to Add Air Native Extension to Stage


I need to display an Activity written in Java using the Android SDK in my Flex Mobile app. I've seen it done with Map ANEs, but cannot find any example code or anything of the sort. I've already created the Java and ActionScript code necessary for my ANE to work (the activity is created and all the classes, events, and methods needed to truly bridge the Java and AS3 is there), I just cannot figure out how to add it to the stage. I know it cannot be added to the DisplayList and I am fine with it being a stage object.

If it helps at all, I am trying to display video using MediaPlayer (due to MP4 streaming issues when it is done using AS3/Flex).

For Michael (Aug 27, 2012 @ 9:44AM MST):

08-27 09:27:07.836: I/CS VideoInit(2567): context is set
08-27 09:27:07.836: I/CS VideoInit(2567): intent is instantiated 
08-27 09:27:07.836: I/ActivityManager(349): START {cmp=air.AndroidANETesting2/xi.video.android.extension.VideoActivity u=0} from pid 2567

The very first line of my Activity is

Log.i("CS VideoActivity","Made it inside the activity somehow");

Here is a look at my Java. This is the init function:

VideoInit.context = context;
Log.i("CS VideoInit","context is set");
Intent intent = new Intent( context.getActivity(), VideoActivity.class );
Log.i("CS VideoInit","intent is instantiated");
context.getActivity().startActivity( intent );
Log.i("CS VideoInit","Activity is started");
context.dispatchStatusEventAsync("PLAY", "PLAY");

And here is my VideoActivity onCreate():

super.onCreate(savedInstanceState);
Log.i("CS VideoActivity","Made it inside the activity somehow");

And my Manifest for good measure (just the application section):

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".VideoActivity"
        android:label="@string/title_activity_video" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

Update (August 27, 2012 @ 10:52AM MST) After further investigation (or trial and error, whatever you want to call it), I decided to throw the startActivity() line into a try{}catch(Throwable e) to see what, if any, errors it was throwing. Interestingly enough, it threw this.

08-27 10:49:41.406: I/CS VideoInit(7786): Unable to find explicit activity class {air.AndroidANETesting2.debug/xi.video.android.extension.VideoActivity}; have you declared this activity in your AndroidManifest.xml?

It would appear I need to recheck my Android Manifest file.


Solution

  • It's actually quite easy.

    You need to create a class in an ANE that implements the android.app.Activity, then from a FREFunction, simply use the startActivity function of the base Activity instance from the FREContext.

    So in a function, lets start an activity with an Intent:

    public class ExampleFunction implements FREFunction 
    {
        @Override
        public FREObject call( FREContext context, FREObject[] passedArgs ) 
        {
            Intent intent = new Intent( context.getActivity(), ExampleActivity.class );
            context.getActivity().startActivity( intent );
        }
    }
    

    Then in the actual Activity implementation:

    public class ExampleActivity extends Activity 
    {
        @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            // Do your stuff here, like create views, buttons etc
        }
    }
    

    This activity will display ontop of your application. You can use a static reference to your FREContext to pass events / data back to your application if you wish.

    You also need to add activity in your -app.xml inside manifest application-tag:

    <application> <activity android:name="package.ExampleActivity"></activity></application>