[Cocos2dx 3.0] How to do Java things with Android Native Activity (AdMob, Analytics, Send/Receive intents, Orientation change etc)

I am a bit concerned about the native activity used in dev branch of 3.0. Maybe it is because I don’t understand native activity completely, or maybe the decision to move to native activity needs more thought.

  • How to integrate AdMob? Java activity could use a XML layout for AdMob with OpenGL/Cocos2dx using rest of the space, how to do this with native activity? On a similar note, how to integrate Google Analytics SDK?
  • How to handle orientation change? Java activity could handle the onSurfaceChanged event? How to do this with native activity?
  • How to send intents and receive intents? Java activity could do startActivityForResult and receive intents in onCreate bundle, how to do this from native activity?

A few quick google searches didn’t give much useful information which makes me wonder if it was a good decision to use native activity after all. I have been using JNI a lot with cocos2dx 2.1.4, but native activity adds a whole level of complexity for seemingly simple tasks.

Is all this even possible using native activity, or should cocos2d go back to Java activity?

After some searching I have found some references, don’t know yet if these will work or not.

Android’s native interface has ANativeActivityCallbacks and it has onNativeWindowResized which will probably get called on orientation change. For sending/receiving intents I found these:
http://stackoverflow.com/questions/12841240/android-pass-parameter-to-native-activity
http://vkswtips.blogspot.in/2012/01/android-how-to-call-java-activity-using.html
http://stackoverflow.com/questions/4080475/android-how-to-broadcast-intent-from-native-code

Still no clues on how AdMob etc can be integrated.

Unfortunately, it seems like AdMob (or any other ads SDK) can’t be integrated for showing ads “inside” native activity. Only possible ways seem to be to:

  1. Show ads on intermediate screens, not on native activity.

  2. Use GLSurfaceView for cocos2d drawing in NativeActivity instead of taking over complete window, but this will need modifying cocos2d code (https://groups.google.com/forum/#!topic/android-ndk/NnunePFoeYg)

  3. Use a Java activity that inherits from NativeActivity, and then show ads in a android.widget.PopupWindow http://www.dynadream.com/ddweb/index.php/Special_Blog?id=20

Have you looked at how 3.0-alpha is doing Android dialogs and modals with NativeActivity?

Might be a place to look.

Sachin Garg wrote:

Unfortunately, it seems like AdMob (or any other ads SDK) can’t be integrated for showing ads “inside” native activity. Only possible ways seem to be to:
>

  1. Show ads on intermediate screens, not on native activity.
    >
  2. Use GLSurfaceView for cocos2d drawing in NativeActivity instead of taking over complete window, but this will need modifying cocos2d code (https://groups.google.com/forum/#!topic/android-ndk/NnunePFoeYg)
    >
  3. Use a Java activity that inherits from NativeActivity, and then show ads in a android.widget.PopupWindow http://www.dynadream.com/ddweb/index.php/Special_Blog?id=20

Hi, we can show ads on native activity by java code. But not layout. You can take a look at the implementation in plugin admob (plugin/plugins/admob). Also there is a sample (plugin/samples/HelloPlugins) can show you how to use the plugin admob.

The resolution in plugin is place the ads at some special position (Center, TopCenter, TopLeft, TopRight, etc.).
I’ve no idea how to place a ads at everywhere I wanted.

Hope it’s helpful for you. :slight_smile:

I suggest you add activity inherit NativeActivity by java code and modification AndroidManifest.xml.
example:
@
package org.cocos2dx;

import android.app.NativeActivity;

public class MyActivity extends NativeActivity{

}
@

You can handle orientation change and receive intents in [MyActivity] now.

Bin Zhang, thanks for the pointer to admob plugin. I checked and it seems very useful and it is already updated for native activity. Looks like all the work to implement AdMob is already done :slight_smile:

WenHai Lin, this is a good idea. By inheriting from NativeActivity we can get back all of Java functionality without making any changes to internal cocos2dx code. But now that I understand native activity better, it seems easier to do things directly using NativeActivity in C++ (by using ANativeActivityCallbacks, **onNativeWindowResized,**onConfigurationChanged etc).

@Sachin Garg
You are right. It is the reason we use Native Activity.

And i think cocos2d-x should add a call back for handling these commands.

@Minggo,
Yeah, many users new to JNI and NativeActivity might not know what to do, for them we can include some example code. Like assign an empty callback for onCreate, and attach the main native thread to the JVM thread etc… I don’t think we will need a cocos2dx callback for onCreate, maybe just some sample code in the generated project.

A really useful cocos2dx callback would be something for orientation change. RIght now iOS and Android callbacks for orientation change work differently (they have different parameters, different ways to register them, etc) and a unified cocos2dx callback will be useful.

Some issues with orientation change: height and width of visible screen area stay the same for full screen apps, but on Android devices like Nexus4 with on-screen buttons or with not-full-screen iOS apps with status bar visible, app window’s visible height and width change on orientation change because of new location of buttons and status bar.

Another issue with IOS is that width and height values are not swapped after orientation change, cocos2dx code will need to manually check if it is landscape or portrait, and then swap the height/width values if needed.

Overall, cocos2dx orientation change callback will need a size parameter (instead of a enum newOrientationID). It will also help cocos2dx to support resizing of app on Mac OSX and Windows desktop.

I will be happy to share my code for this if you need.

@Sachin Garg
Thank you for your feedback.
You are appreciated for sharing your code.

About samples of Nativity Activity usage. Could you please write a detail requirements. We are happy to add them.

I updated my orientation change code for 3.0 API and NativeActivity and copied code here:

http://www.cocos2d-x.org/issues/2883

I’d like to support multiple orientations for my app.
Does anyone know how to resize opengl surface on orientation change?
Simply calling `setFrameSize` and `setDesignResolutionSize` doesn’t achieve this goal.

After you do setFrameSize and setDesignResolution, you can try to start your current scene again.

Only your scene’s init function knows where to place your sprites. You might also need to remember and restore sprite’s current state so that everything is where user expects them to be. Another option is to break your init into two functions, init can create the sprites and add another function to place the sprites based on screen size. On orientation change you will need to call this second function to re-position your scene.

Hi guys, how to get current app version with versionName in Native Activity, I am a newbie with JNI, i found this function could be useful:
@ private static void initVersion(Context context){
try {
PackageManager pm = context.getPackageManager();
PackageInfo pi = pm.getPackageInfo(context.getPackageName(), 0);
vercode = “”+pi.versionCode;
vername = pi.versionName;
} catch (PackageManager.NameNotFoundException e) {
vercode = “1”;
vername = “1.1”;
}
}@
But I have no idea how to get context in Native Activity? Can you help me? Thank you very much. Sachin GargBin Zhang @Minggo Zhang

Many android programmers think that it is impossible to call a java activity using intents from a purely native NDK application (native activity as the MAIN launcher activity).

It is NOT true.

But it is not a straight forward though and is little tricky.
People think that it is impossible because they think that context will not be there for pure native NDK applications and hence we cannot launch any java activity.
Following is the code snippet which can be used to get the context in a purely native NDK application code.

Source: Java Training in Delhi | E-java Learning

@tanya singh
Thanks for sharing. As you can see in beta version, we added java activity in cocos2d-x samples to show how to inherit from native activity.

Minggo Zhang wrote:

@tanya singh
Thanks for sharing. As you can see in beta version, we added java activity in cocos2d-x samples to show how to inherit from native activity.

Hi,master,can you give me some tips on this topic:

And i test the testcpp (version 2.2.1) on android devices,the question is be finded too,so i search many tips and try it, but it is go on,Can you help me and give me some tips!

you can import the sdk in java. and using jni to communicate to c++.

I’ll give you a simple tutaorial about Jni. if u to know more detail. just read this topic:

Sachin Garg wrote:

I am a bit concerned about the native activity used in dev branch of 3.0. Maybe it is because I don’t understand native activity completely, or maybe the decision to move to native activity needs more thought.
>
* How to integrate AdMob? Java activity could use a XML layout for AdMob with OpenGL/Cocos2dx using rest of the space, how to do this with native activity? On a similar note, how to integrate Google Analytics SDK?
* How to handle orientation change? Java activity could handle the onSurfaceChanged event? How to do this with native activity?
* How to send intents and receive intents? Java activity could do startActivityForResult and receive intents in onCreate bundle, how to do this from native activity?
>
A few quick google searches didn’t give much useful information which makes me wonder if it was a good decision to use native activity after all. I have been using JNI a lot with cocos2dx 2.1.4, but native activity adds a whole level of complexity for seemingly simple tasks.
>
Is all this even possible using native activity, or should cocos2d go back to Java activity?