[SDL] SDL2 on Android: android_app struct pointer

Martin Gerhardy martin.gerhardy at gmail.com
Fri Oct 11 22:50:08 PDT 2013


Am 11.10.2013 14:20, schrieb glezmen:
> Hi everyone,
>
> I'd like to add AdMob ads to my almost-done game. I found a great 
> howto: http://www.dynadream.com/ddweb/index.php/Special_Blog?id=20
> The problem is, I can't find out how to get the android_app struct 
> pointer needed for the last step:
>
>
>
>
>
>
>
>
> Code:
>
>  // Somewhere you defined this...
> extern struct android_app *__gandroidapp__;
>
>
>
> I search3e the complete SDL sources for android_app, tried to get it 
> somehow through JNI_GetEnv, but still nothing Sad
>
> can someone enlighten me on this topic?
>

Hi,

I think this is in
#include <android_native_app_glue.h>

but you can also just use jni to call some method in your activity like 
this:

in your c++ code

JNIEnv *env = static_cast<JNIEnv*>(SDL_AndroidGetJNIEnv());
jobject activity = static_cast<jobject>(SDL_AndroidGetActivity());
jclass cls = env->GetObjectClass(activity);
jclass localcls = reinterpret_cast<jclass>(env->NewGlobalRef(cls))
jmethodID showAds = env->GetStaticMethodID(localcls, "showAds", "(Z)V");
jmethodID hideAds = env->GetStaticMethodID(localcls, "hideAds", "()V");

bool onTop = true;
bool show = true;
if (show) {
     env->CallStaticVoidMethod(localcls, showAds, ontop);
} else {
     env->CallStaticVoidMethod(localcls, hideAds);
}

env->DeleteGlobalRef(localcls);


in your java activity class:
public static void showAds(final boolean ontop) {
  [...]
}

public static void hideAds() {
  [...]
}

These are just stripped down lines from my code - so i'm sure it 
compiles - but you should see how it works, you should of course cache 
the localcls, the env and the methodids
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20131012/de3f77e4/attachment-0009.htm>


More information about the SDL mailing list