本页介绍了如何处理来自后台线程的回调。
Google 移动广告 SDK(Beta 版)在后台线程上运行广告加载和事件回调。在这些回调中执行与界面相关的操作时,请确保明确将它们调度到界面线程。
以下示例在广告加载后将横幅广告视图添加到视图层次结构中:
Kotlin
BannerAd.load( adRequest, object : AdLoadCallback<BannerAd> { override fun onAdLoaded(ad: BannerAd) { // Add the banner view to the view hierarchy on the UI thread. activity?.runOnUiThread { binding.bannerViewContainer.addView(ad.getView(requireActivity())) } } }, )
Java
BannerAd.load( adRequest, new AdLoadCallback<BannerAd>() { @Override public void onAdLoaded(@NonNull BannerAd ad) { // Add the banner view to the view hierarchy on the UI thread. runOnUiThread( () -> binding.bannerViewContainer.addView(ad.getView(MainActivity.this))); } });