0

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.

Josh
  • 8,079
  • 3
  • 24
  • 49
  • Without knowing details of the ANE; I'm not sure this is answerable. Generally, an ANE is just a special type of library file. So, you wouldn't add an ANE to the stage; but rather a class inside the ANE. I don't underestand how adding something to the stage is different than adding something to the DisplayList. – JeffryHouser Aug 22 '12 at 18:28
  • Adding something to the stage means it floats above everything else. Adding something to the display list is generally how you add things. Think of how you use stageWebView. You set its stage and a viewport. The Viewport is positioned relative to the stage, not to the parent like a normal Flex component. In addition, nothing can be displayed above that stage item, so you have to position differently and work around object that would normally be displayed on top of that object (such as settings menu for the player). – Josh Aug 22 '12 at 18:37
  • And it is answerable, as it has been done before and several Adobe platform evangelists have stated you can display an Android Activity (or its iOS counterpart) using a Native Extension. Here is a video of it being done. http://www.flashmobileblog.com/2011/09/15/air-3-mapview-native-extension-on-ios/ – Josh Aug 22 '12 at 18:48
  • If you have already created the ANE; as you seem to state in your post; I don't see how we can provide you with details on how to use it. Perhaps you need to rework your post? For info about the Stage; please read: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html & http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#stage & http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html#addChild%28%29 . – JeffryHouser Aug 22 '12 at 19:53
  • As I understand it; Stage is, essentially, at the top of the DisplayList. I've never heard of anyone trying to use it to, in essence, create a second display list (Aside from the stuff built in such as StageVideo, Stage3D, and StageWebView ). but, in theory it sounds possible. Despite some samples out there, ANEs were not built for visual display functionality and most recommendations I've seen were to not use ANEs for visual display purposes. – JeffryHouser Aug 22 '12 at 19:56

1 Answers1

5

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>
Deko
  • 1,028
  • 2
  • 15
  • 24
Michael
  • 3,776
  • 1
  • 16
  • 27
  • This is excellent. I will test this tomorrow morning and get back to you about it, but this definitely looks like it will do what I need it to. Thanks! – Josh Aug 23 '12 at 04:29
  • Okay, I was able to bridge the ANE to my AS3 successfully... kinda. I run your script within my init function and the script stops at this line and I cannot figure out why. If I comment it out, my return data and event dispatch that occurs after that line happens just fine. Any idea? "context.getActivity().startActivity( intent );" is the line – Josh Aug 24 '12 at 02:06
  • By "init" do you mean the extension initialiser or do you mean a custom FREFunction that you're calling init? You shouldn't call the above code in the extension initialiser. – Michael Aug 24 '12 at 23:00
  • Sorry, it's in a FREFunction that I am calling init and calling immediately after setting the extension context in my AS3. I should have been clearer about that. – Josh Aug 24 '12 at 23:40
  • Can you get any information from the logs? Run the following in a terminal : [flexlib]/lib/android/bin/adb logcat – Michael Aug 24 '12 at 23:42
  • It doesn't make it into the activity. I'll edit the OP with the log. – Josh Aug 27 '12 at 16:28
  • Ok, code is up. Looks like it doesn't make it into the onCreate at all, though the Activity Manager does seem to start the activity. Placement of the log line (before or after the super.onCreate() line) makes no difference (and I did not think it would, but it was worth a shot) – Josh Aug 27 '12 at 16:55
  • I just ran it as a Java Debug (as opposed to Flash debug) and it makes it inside that activity just fine. – Josh Aug 27 '12 at 16:58
  • Try adding the activity to your application descriptor, manifest additions: – Michael Aug 27 '12 at 22:59
  • I managed to figure it out on my own about an hour and a half ago. Working on getting a SurfaceHolder working now. Just to be clear, you have to add the activity to your FLEX app descriptor, NOT the Android Manifest. I spent 4+ hours figuring that one out. Either way, many thanks for your help. I've almost got this working now. – Josh Aug 27 '12 at 23:47
  • Yeah was just about to say that, it's a common mistake sorry, should have pointed that one out in the answer. – Michael Aug 28 '12 at 00:38