Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AdMob Ads not correctly showing in Android SDL app

I encountered a problem when implementing AdMob advertising in my app. I'm using SDL and native code. Ad is created in onCreate method like this:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    Log.v("SDL", "onCreate():" + mSingleton);
    super.onCreate(savedInstanceState);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

    SDLActivity.initialize();
    mSingleton = this;

    mSurface = new SDLSurface(getApplication());
    mLayout = new RelativeLayout(this);

    adView = new AdView( this );
    adView.setAdUnitId("ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX");
    adView.setAdSize(AdSize.BANNER);

    request = new AdRequest.Builder()
    .addTestDevice("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
    .build();

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    adView.loadAd(request);

    mLayout.addView(mSurface);
    mLayout.addView(adView, params);

    setContentView(mLayout);

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

I think that this code is correct, because the ad is located on the screen, but it is not visible. I can touch the screen on location where the ad should be and the browser starts to open, so the ad is actually loaded.

Then the ad will appear correct when:

  • it's time to refresh (set on AdMob account, I have set 30 sec)
  • I quit the app and then resume, when the onPause and onResume methods gets called from the Activity class

I was thinking about some refresh methods, but can't find any to solve this problem. This methods I tried:

adView.refreshDrawableState();
adView.setActivated( true );
adView.setEnabled( true );
adView.setFocusable( true );
adView.setHovered( true );
adView.showContextMenu();

I have no idea what else can be done. Any suggestions?

Update

I have checked my logcat output, but I don't see nothing strange there:

1.  I/ActivityManager(  644): START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=org.libsdl.app/.SDLActivity bnds=[184,839][360,1062]} from pid 12651
2.  I/ActivityManager(  644): Start proc org.libsdl.app for activity org.libsdl.app/.SDLActivity: pid=24024 uid=10123 gids={50123, 3003, 1028, 1015}
3.  W/linker  (24024): libopenal.so has text relocations. This is wasting memory and is a security risk. Please fix.
4.  I/LibraryLoader(24024): Expected native library version number "",actual native library version number ""
5.  I/chromium(24024): [INFO:library_loader_hooks.cc(116)] Chromium logging enabled: level = 0, default verbosity = 0
6.  I/BrowserStartupController(24024): Initializing chromium process, renderers=0
7.  E/AudioManagerAndroid(24024): BLUETOOTH permission is missing!
8.  W/Adreno-GSL(24024): <get_panel_settings:3802>: Android framework reported version 2. So, don't force ES30
9.  I/Adreno-EGL(24024): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
10. I/Adreno-EGL(24024): OpenGL ES Shader Compiler Version: 17.01.10.SPL
11. I/Adreno-EGL(24024): Build Date: 11/06/13 Wed
12. I/Adreno-EGL(24024): Local Branch: 
13. I/Adreno-EGL(24024): Remote Branch: 
14. I/Adreno-EGL(24024): Local Patches: 
15. I/Adreno-EGL(24024): Reconstruct Branch: 
16. I/Ads     (24024): Starting ad request.
17. I/ActivityManager(  644): Displayed org.libsdl.app/.SDLActivity: +1s113ms
18. I/ActivityManager(  644): Process com.android.musicfx (pid 18286) has died.
19. E/rsC++   (12651): RS Message thread exiting.
20. I/OpenAL  (24024): _SC_NPROCESSORS_ONLN=2
21. I/dalvikvm-heap(10840): Grow heap (frag case) to 6.923MB for 1127536-byte allocation
22. I/chromium(10840): [INFO:CONSOLE(0)] "Document was loaded from Application Cache with manifest https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.appcache", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0)
23. I/chromium(10840): [INFO:CONSOLE(0)] "Application Cache Checking event", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0)
24. I/chromium(10840): [INFO:CONSOLE(0)] "Application Cache NoUpdate event", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0)
25. I/dalvikvm-heap(24024): Grow heap (frag case) to 4.740MB for 1127536-byte allocation
26. W/chromium(24024): [WARNING:proxy_service.cc(890)] PAC support disabled because there is no system implementation
27. I/Ads     (24024): Scheduling ad refresh 60000 milliseconds from now.
28. I/Ads     (24024): Ad finished loading.
29. W/ActivityThread(24024): ClassLoader.loadClass: The class loader returned by Thread.getContextClassLoader() may fail for processes that host multiple applications. You should explicitly specify a context class loader. For example: Thread.setContextClassLoader(getClass().getClassLoader());
30. W/KInfoc  (23003): The data section count is not equal! table:fm_public, count in format:13, count in data:14.
31. W/KInfoc  (23003): The data section count is not equal! table:fm_public, count in format:13, count in data:14.
32. I/wpa_supplicant(25610): wlan0: WPA: Group rekeying completed with 00:21:27:da:a8:64 [GTK=TKIP]
33. W/Uploader(29884): No account for auth token provided
34. W/Uploader(29884): No account for auth token provided
35. W/Uploader(29884): No account for auth token provided
36. I/wpa_supplicant(25610): wlan0: WPA: Group rekeying completed with 00:21:27:da:a8:64 [GTK=TKIP]
37. I/Ads     (24024): Starting ad request.
38. I/chromium(10840): [INFO:CONSOLE(0)] "Document was loaded from Application Cache with manifest https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.appcache", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0)
39. I/chromium(10840): [INFO:CONSOLE(0)] "Application Cache Checking event", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0)
40. I/chromium(10840): [INFO:CONSOLE(0)] "Application Cache NoUpdate event", source: https://googleads.g.doubleclick.net/mads/static/mad/sdk/native/sdk-core-v40.html (0)
41. I/Ads     (24024): Scheduling ad refresh 60000 milliseconds from now.
42. I/Ads     (24024): Ad finished loading.
43. W/AwContents(24024): nativeOnDraw failed; clearing to background color.

On line 16 the ad is starting a request and on line 28 there's a message that ad has finished loading, but its not shown on the screen. After 60 seconds is started another request (line 37) and when is complete the ad appears on the screen. On the last line there is a message saying that nativeOnDraw failed, I don't know if it has something to do with it?

like image 501
Tom Avatar asked Feb 07 '15 21:02

Tom


2 Answers

I solved it just by setting background color:

adView.loadAd(request);
adView.setBackgroundColor(Color.TRANSPARENT);

mLayout.addView(mSurface);
mLayout.addView(adView, params);
like image 93
Tom Avatar answered Sep 25 '22 01:09

Tom


Could you try to change this order?

adView.loadAd(request);

mLayout.addView(mSurface);
mLayout.addView(adView, params);

To:

mLayout.addView(mSurface);
mLayout.addView(adView, params);

adView.loadAd(request);

Because your View isn't added in a ViewGroup, so might be a problem at your first run. The thing is, AdMob is quite smart, it checks if the AdView and the parents are visible. So, because you're not having any parents at that moment, it schedules to check after X seconds. This is why you're seeing after a while.

like image 22
Lucas Avatar answered Sep 27 '22 01:09

Lucas