AdMob广告未在Android SDL应用中正确显示

时间:2015-02-07 21:01:25

标签: android opengl-es java-native-interface admob sdl

在我的应用中实施AdMob广告时遇到了问题。我正在使用SDL和本机代码。广告以onCreate方法创建,如下所示:

@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);
}

我认为此代码是正确的,因为广告位于屏幕上,但不可见。我可以在广告所在的位置触摸屏幕并且浏览器开始打开,因此广告实际上已加载。

然后,广告在以下情况下显示正确:

  • 是时候刷新了(在AdMob帐户上设置,我设置了30秒)
  • 当我从onPause
  • 调用onResumeActivity方法时,我退出应用然后恢复
我正在考虑一些刷新方法,但找不到任何解决这个问题的方法。我尝试过这种方法:

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

我不知道还能做些什么。有什么建议吗?

更新

我检查了我的logcat输出,但我没有看到任何奇怪的事情:

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.

第16行广告正在启动请求,第28行显示广告已完成加载的消息,但未在屏幕上显示。 在60秒后开始另一个请求(第37行),当完成时,广告出现在屏幕上。在最后一行有一条消息说nativeOnDraw失败了,我不知道它是否与它有关?

2 个答案:

答案 0 :(得分:3)

我只是通过设置背景颜色来解决它:

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

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

答案 1 :(得分:2)

您可以尝试更改此订单吗?

adView.loadAd(request);

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

要:

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

adView.loadAd(request);

由于您的View未添加到ViewGroup中,因此首次运行时可能会出现问题。 问题是,AdMob非常聪明,它会检查AdView和父母是否可见。所以,因为你当时没有任何父母,所以它安排在X秒后检查。这就是你在一段时间后看到的原因。