AdMob 广告未在 Android SDL 应用程序中正确显示
AdMob Ads not correctly showing in Android SDL app
我在我的应用中实施 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秒)
- 当从
Activity
class 调用 onPause
和 onResume
方法时,我退出应用程序然后继续
我一直在想一些刷新的方法,但找不到解决这个问题的方法。我试过的方法:
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
failed,不知道跟这个有没有关系?
您可以尝试更改此顺序吗?
adView.loadAd(request);
mLayout.addView(mSurface);
mLayout.addView(adView, params);
收件人:
mLayout.addView(mSurface);
mLayout.addView(adView, params);
adView.loadAd(request);
因为您的视图未添加到 ViewGroup 中,所以您的第一个 运行 可能会出现问题。
问题是,AdMob 非常聪明,它会检查 AdView 和 parents 是否可见。因此,因为您当时没有任何 parents,它会安排在 X 秒后进行检查。这就是为什么你会在一段时间后看到。
我只是通过设置背景颜色解决了它:
adView.loadAd(request);
adView.setBackgroundColor(Color.TRANSPARENT);
mLayout.addView(mSurface);
mLayout.addView(adView, params);
我在我的应用中实施 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秒)
- 当从
Activity
class 调用
onPause
和 onResume
方法时,我退出应用程序然后继续
我一直在想一些刷新的方法,但找不到解决这个问题的方法。我试过的方法:
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
failed,不知道跟这个有没有关系?
您可以尝试更改此顺序吗?
adView.loadAd(request);
mLayout.addView(mSurface);
mLayout.addView(adView, params);
收件人:
mLayout.addView(mSurface);
mLayout.addView(adView, params);
adView.loadAd(request);
因为您的视图未添加到 ViewGroup 中,所以您的第一个 运行 可能会出现问题。 问题是,AdMob 非常聪明,它会检查 AdView 和 parents 是否可见。因此,因为您当时没有任何 parents,它会安排在 X 秒后进行检查。这就是为什么你会在一段时间后看到。
我只是通过设置背景颜色解决了它:
adView.loadAd(request);
adView.setBackgroundColor(Color.TRANSPARENT);
mLayout.addView(mSurface);
mLayout.addView(adView, params);