本頁說明如何從背景執行緒處理回呼。
Next Gen Mobile Ads SDK 會在背景執行緒上執行廣告載入和事件回呼。在這些回呼中執行 UI 相關作業時,請務必明確將這些作業調度至 UI 執行緒。
以下範例會在廣告載入後,將橫幅廣告檢視畫面新增至檢視區塊階層:
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))); } });