Skip to content

Commit e196f54

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Updated snippets.
PiperOrigin-RevId: 904133504
1 parent d45feba commit e196f54

2 files changed

Lines changed: 256 additions & 0 deletions

File tree

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
* Copyright 2026 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.gms.snippets;
18+
19+
import android.app.Activity;
20+
import androidx.annotation.NonNull;
21+
import androidx.annotation.Nullable;
22+
import com.google.android.gms.ads.AdError;
23+
import com.google.android.gms.ads.ResponseInfo;
24+
import com.google.android.gms.ads.preload.PreloadCallbackV2;
25+
import com.google.android.gms.ads.preload.PreloadConfiguration;
26+
import com.google.android.gms.ads.rewarded.RewardedAd;
27+
import com.google.android.gms.ads.rewarded.RewardedAdPreloader;
28+
29+
/** Java code snippets for the developer guide. */
30+
final class RewardedAdPreloaderSnippets {
31+
// Sample app rewarded ad unit ID.
32+
public static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";
33+
34+
private void startPreload() {
35+
// [START start_preload]
36+
// Define a PreloadConfiguration.
37+
PreloadConfiguration configuration = new PreloadConfiguration.Builder(AD_UNIT_ID).build();
38+
// Start the preloading with a given preload ID, preload configuration.
39+
RewardedAdPreloader.start(AD_UNIT_ID, configuration);
40+
// [END start_preload]
41+
}
42+
43+
private void startPreloadWithCallback() {
44+
// [START start_preload_callback]
45+
// Define a PreloadConfiguration.
46+
PreloadConfiguration configuration = new PreloadConfiguration.Builder(AD_UNIT_ID).build();
47+
48+
// [START set_callback]
49+
// Define a callback to receive preload events.
50+
PreloadCallbackV2 callback =
51+
new PreloadCallbackV2() {
52+
@Override
53+
public void onAdPreloaded(
54+
@NonNull String preloadId, @Nullable ResponseInfo responseInfo) {
55+
// Called when preloaded ads are available.
56+
}
57+
58+
@Override
59+
public void onAdsExhausted(@NonNull String preloadId) {
60+
// Called when no preloaded ads are available.
61+
}
62+
63+
@Override
64+
public void onAdFailedToPreload(@NonNull String preloadId, @NonNull AdError adError) {
65+
// Called when preloaded ads failed to load.
66+
}
67+
};
68+
// [END set_callback]
69+
70+
// Start the preloading with a given preload ID, preload configuration, and callback.
71+
RewardedAdPreloader.start(AD_UNIT_ID, configuration, callback);
72+
// [END start_preload_callback]
73+
}
74+
75+
private void bufferSize() {
76+
// [START set_buffer_size]
77+
// Define a PreloadConfiguration and buffer up to 5 preloaded ads.
78+
PreloadConfiguration configuration =
79+
new PreloadConfiguration.Builder(AD_UNIT_ID).setBufferSize(5).build();
80+
// [END set_buffer_size]
81+
}
82+
83+
private void pollAndShowAd(Activity activity) {
84+
// [START pollAndShowAd]
85+
// pollAd() returns the next available ad and loads another ad in the background.
86+
RewardedAd ad = RewardedAdPreloader.pollAd(AD_UNIT_ID);
87+
88+
if (ad != null) {
89+
// [Optional] Interact with the ad object as needed.
90+
ad.setOnPaidEventListener(
91+
adValue -> {
92+
// [Optional] Send the impression-level ad revenue information to your preferred
93+
// analytics server directly within this callback.
94+
});
95+
96+
// Show the ad immediately.
97+
ad.show(activity, rewardItem -> {});
98+
}
99+
// [END pollAndShowAd]
100+
}
101+
102+
private void isAdAvailable() {
103+
// [START isAdAvailable]
104+
// Verify that a preloaded ad is available.
105+
if (!RewardedAdPreloader.isAdAvailable(AD_UNIT_ID)) {
106+
// No ads are available to show.
107+
}
108+
// [END isAdAvailable]
109+
}
110+
111+
private void numOfAdsAvailable() {
112+
// [START numOfAdsAvailable]
113+
// Check the number of ads available to show.
114+
int numberOfAds = RewardedAdPreloader.getNumAdsAvailable(AD_UNIT_ID);
115+
// [END numOfAdsAvailable]
116+
}
117+
118+
private void getConfiguration() {
119+
// [START getConfiguration]
120+
PreloadConfiguration configuration = RewardedAdPreloader.getConfiguration(AD_UNIT_ID);
121+
// [END getConfiguration]
122+
}
123+
124+
private void destroyAd() {
125+
// [START destroy_ad]
126+
// Stops the preloading and destroys preloaded ads.
127+
RewardedAdPreloader.destroy(AD_UNIT_ID);
128+
// Stops the preloading and destroys all ads.
129+
RewardedAdPreloader.destroyAll();
130+
// [END destroy_ad]
131+
}
132+
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
* Copyright 2025 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.android.gms.snippets
18+
19+
import android.app.Activity
20+
import androidx.fragment.app.Fragment
21+
import com.google.android.gms.ads.AdError
22+
import com.google.android.gms.ads.ResponseInfo
23+
import com.google.android.gms.ads.preload.PreloadCallbackV2
24+
import com.google.android.gms.ads.preload.PreloadConfiguration
25+
import com.google.android.gms.ads.rewarded.RewardedAdPreloader
26+
27+
/** Kotlin code snippets for the developer guide. */
28+
class RewardedAdPreloaderSnippets : Fragment() {
29+
// Sample app rewarded ad unit ID.
30+
val AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917"
31+
32+
private fun startPreload() {
33+
// [START start_preload]
34+
// Define a PreloadConfiguration.
35+
val configuration = PreloadConfiguration.Builder(AD_UNIT_ID).build()
36+
// Start the preloading with a given preload ID, preload configuration.
37+
RewardedAdPreloader.start(AD_UNIT_ID, configuration)
38+
// [END start_preload]
39+
}
40+
41+
private fun startPreloadWithCallback() {
42+
// [START start_preload_callback]
43+
// Define a PreloadConfiguration.
44+
val configuration = PreloadConfiguration.Builder(AD_UNIT_ID).build()
45+
46+
// [START set_callback]
47+
// Define a callback to receive preload events.
48+
val callback =
49+
object : PreloadCallbackV2() {
50+
override fun onAdPreloaded(preloadId: String, responseInfo: ResponseInfo?) {
51+
// Called when preloaded ads are available.
52+
}
53+
54+
override fun onAdsExhausted(preloadId: String) {
55+
// Called when no preloaded ads are available.
56+
}
57+
58+
override fun onAdFailedToPreload(preloadId: String, adError: AdError) {
59+
// Called when preloaded ads failed to load.
60+
}
61+
}
62+
// [END set_callback]
63+
64+
// Start the preloading with a given preload Id, preload configuration, and callback.
65+
RewardedAdPreloader.start(AD_UNIT_ID, configuration, callback)
66+
// [END start_preload_callback]
67+
}
68+
69+
private fun bufferSize() {
70+
// [START set_buffer_size]
71+
// Define a PreloadConfiguration and buffer up to 5 preloaded ads.
72+
val configuration = PreloadConfiguration.Builder(AD_UNIT_ID).setBufferSize(5).build()
73+
// [END set_buffer_size]
74+
}
75+
76+
private fun pollAndShowAd(activity: Activity) {
77+
// [START pollAndShowAd]
78+
// pollAd() returns the next available ad and loads another ad in the background.
79+
val ad = RewardedAdPreloader.pollAd(AD_UNIT_ID)
80+
81+
if (ad != null) {
82+
// [Optional] Interact with the ad object as needed.
83+
ad.setOnPaidEventListener { adValue ->
84+
// [Optional] Send the impression-level ad revenue information to your preferred
85+
// analytics server directly within this callback.
86+
}
87+
88+
// Show the ad immediately.
89+
ad.show(activity) { rewardItem -> }
90+
}
91+
// [END pollAndShowAd]
92+
}
93+
94+
private fun isAdAvailable() {
95+
// [START isAdAvailable]
96+
// Verify that a preloaded ad is available.
97+
if (!RewardedAdPreloader.isAdAvailable(AD_UNIT_ID)) {
98+
// No ads are available to show.
99+
}
100+
// [END isAdAvailable]
101+
}
102+
103+
private fun numOfAdsAvailable() {
104+
// [START numOfAdsAvailable]
105+
// Check the number of ads available to show.
106+
val numberOfAds = RewardedAdPreloader.getNumAdsAvailable(AD_UNIT_ID)
107+
// [END numOfAdsAvailable]
108+
}
109+
110+
private fun getConfiguration() {
111+
// [START getConfiguration]
112+
val configuration = RewardedAdPreloader.getConfiguration(AD_UNIT_ID)
113+
// [END getConfiguration]
114+
}
115+
116+
private fun destroyAd() {
117+
// [START destroy_ad]
118+
// Stops the preloading and destroy preloaded ads.
119+
RewardedAdPreloader.destroy(AD_UNIT_ID)
120+
// Stops the preloading and destroy all ads.
121+
RewardedAdPreloader.destroyAll()
122+
// [END destroy_ad]
123+
}
124+
}

0 commit comments

Comments
 (0)