Skip to content

Commit 5b92f73

Browse files
nventimiglicopybara-github
authored andcommitted
Internal changes.
PiperOrigin-RevId: 760680382
1 parent 6dfd24d commit 5b92f73

File tree

2 files changed

+56
-51
lines changed

2 files changed

+56
-51
lines changed

java/advanced/APIDemo/app/src/main/java/com/google/android/gms/example/apidemo/preloading/AdMobPreloadingAdsFragment.java

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class AdMobPreloadingAdsFragment extends PreloadItemFragment {
4141

4242
@Override
4343
public View onCreateView(
44-
LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
44+
@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
4545
// Inflate the layout.
4646
FragmentPreloadItemBinding viewBinding =
4747
FragmentPreloadItemBinding.inflate(getLayoutInflater());
@@ -57,44 +57,50 @@ public View onCreateView(
5757

5858
// [START start_preload]
5959
private void startPreload() {
60-
// Define the number of ads that can be preloaded for each ad unit.
61-
int bufferSize = 2;
62-
// Define a list of PreloadConfiguration objects, specifying the ad unit ID and ad format for
63-
// each ad unit to be preloaded.
60+
// Define a list of PreloadConfiguration objects, specifying the ad unit ID and ad format.
6461
List<PreloadConfiguration> preloadConfigs =
6562
Arrays.asList(
6663
new PreloadConfiguration.Builder(InterstitialFragment.AD_UNIT_ID, AdFormat.INTERSTITIAL)
67-
.setBufferSize(bufferSize)
6864
.build(),
6965
new PreloadConfiguration.Builder(RewardedFragment.AD_UNIT_ID, AdFormat.REWARDED)
70-
.setBufferSize(bufferSize)
7166
.build(),
7267
new PreloadConfiguration.Builder(AppOpenFragment.AD_UNIT_ID, AdFormat.APP_OPEN_AD)
73-
.setBufferSize(bufferSize)
7468
.build());
7569

76-
// Define a callback to receive preload availability events.
77-
PreloadCallback callback =
78-
new PreloadCallback() {
79-
@Override
80-
public void onAdsAvailable(@NonNull PreloadConfiguration preloadConfig) {
81-
Log.i(LOG_TAG, "Preload ad for " + preloadConfig.getAdFormat() + " is available.");
82-
updateUI();
83-
}
84-
85-
@Override
86-
public void onAdsExhausted(@NonNull PreloadConfiguration preloadConfig) {
87-
Log.i(LOG_TAG, "Preload ad for " + preloadConfig.getAdFormat() + " is exhausted.");
88-
updateUI();
89-
}
90-
};
91-
9270
// Start the preloading initialization process.
93-
MobileAds.startPreload(this.requireContext(), preloadConfigs, callback);
71+
// Be sure to pass an Activity context when calling startPreload() if you use mediation, as
72+
// some mediation partners require an Activity context to load ads.
73+
// Optional : Define a callback to receive preload availability events.
74+
MobileAds.startPreload(this.requireContext(), preloadConfigs, getPreloadCallback());
9475
}
9576

9677
// [END start_preload]
9778

79+
// [START set_callback]
80+
private PreloadCallback getPreloadCallback() {
81+
// Define a callback to receive preload availability events.
82+
return new PreloadCallback() {
83+
@Override
84+
public void onAdsAvailable(@NonNull PreloadConfiguration preloadConfig) {
85+
Log.i(
86+
LOG_TAG,
87+
"Preload ad for configuration " + preloadConfig.getAdFormat() + " is available.");
88+
// [START_EXCLUDE]
89+
updateUI();
90+
// [END_EXCLUDE]
91+
}
92+
93+
@Override
94+
public void onAdsExhausted(@NonNull PreloadConfiguration preloadConfig) {
95+
Log.i(
96+
LOG_TAG,
97+
"Preload ad for configuration " + preloadConfig.getAdFormat() + " is exhausted.");
98+
}
99+
};
100+
}
101+
102+
// [END set_callback]
103+
98104
private void addFragmentsUI() {
99105
addFragmentUI(new AppOpenFragment());
100106
addFragmentUI(new InterstitialFragment());

kotlin/advanced/APIDemo/app/src/main/java/com/google/android/gms/example/apidemo/preloading/AdMobPreloadingAdsFragment.kt

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,43 +53,42 @@ class AdMobPreloadingAdsFragment : Fragment() {
5353

5454
// [START start_preload]
5555
private fun startPreload() {
56-
// Define the number of ads that can be preloaded for each ad unit.
57-
val bufferSize = 2
58-
// Define a list of PreloadConfiguration objects, specifying the ad unit ID and ad format for
59-
// each ad unit to be preloaded.
56+
// Define a list of PreloadConfiguration objects, specifying the ad unit ID and ad format.
6057
val preloadConfigs =
6158
listOf(
6259
PreloadConfiguration.Builder(InterstitialFragment.AD_UNIT_ID, AdFormat.INTERSTITIAL)
63-
.setBufferSize(bufferSize)
64-
.build(),
65-
PreloadConfiguration.Builder(RewardedFragment.AD_UNIT_ID, AdFormat.REWARDED)
66-
.setBufferSize(bufferSize)
67-
.build(),
68-
PreloadConfiguration.Builder(AppOpenFragment.AD_UNIT_ID, AdFormat.APP_OPEN_AD)
69-
.setBufferSize(bufferSize)
7060
.build(),
61+
PreloadConfiguration.Builder(RewardedFragment.AD_UNIT_ID, AdFormat.REWARDED).build(),
62+
PreloadConfiguration.Builder(AppOpenFragment.AD_UNIT_ID, AdFormat.APP_OPEN_AD).build(),
7163
)
7264

73-
// Define a callback to receive preload availability events.
74-
val callback =
75-
object : PreloadCallback {
76-
override fun onAdsAvailable(preloadConfig: PreloadConfiguration) {
77-
Log.i(LOG_TAG, "Preload ad for ${ preloadConfig.adFormat } is available.")
78-
updateUI()
79-
}
80-
81-
override fun onAdsExhausted(preloadConfig: PreloadConfiguration) {
82-
Log.i(LOG_TAG, "Preload ad for configuration ${ preloadConfig.adFormat } is exhausted.")
83-
updateUI()
84-
}
85-
}
86-
8765
// Start the preloading initialization process.
88-
MobileAds.startPreload(this.requireContext(), preloadConfigs, callback)
66+
// Be sure to pass an Activity context when calling startPreload() if you use mediation, as
67+
// some mediation partners require an Activity context to load ads.
68+
// Optional : Define a callback to receive preload availability events.
69+
MobileAds.startPreload(this.requireContext(), preloadConfigs, getPreloadCallback())
8970
}
9071

9172
// [END start_preload]
9273

74+
// [START set_callback]
75+
private fun getPreloadCallback(): PreloadCallback {
76+
return object : PreloadCallback {
77+
override fun onAdsAvailable(preloadConfig: PreloadConfiguration) {
78+
Log.i(LOG_TAG, "Preload ad for configuration ${preloadConfig.adFormat} is available.")
79+
// [START_EXCLUDE]
80+
updateUI()
81+
// [END_EXCLUDE]
82+
}
83+
84+
override fun onAdsExhausted(preloadConfig: PreloadConfiguration) {
85+
Log.i(LOG_TAG, "Preload ad for configuration ${preloadConfig.adFormat} is exhausted.")
86+
}
87+
}
88+
}
89+
90+
// [END set_callback]
91+
9392
private fun addFragments() {
9493
addFragment(AppOpenFragment())
9594
addFragment(InterstitialFragment())

0 commit comments

Comments
 (0)