Skip to content

Commit f59d415

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

2 files changed

Lines changed: 257 additions & 0 deletions

File tree

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
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.annotation.NonNull;
21+
import androidx.annotation.Nullable;
22+
import androidx.fragment.app.Fragment;
23+
import com.google.android.gms.ads.AdError;
24+
import com.google.android.gms.ads.ResponseInfo;
25+
import com.google.android.gms.ads.preload.PreloadCallbackV2;
26+
import com.google.android.gms.ads.preload.PreloadConfiguration;
27+
import com.google.android.gms.ads.rewarded.RewardedAd;
28+
import com.google.android.gms.ads.rewarded.RewardedAdPreloader;
29+
30+
/** Java code snippets for the developer guide. */
31+
final class RewardedAdPreloaderSnippets extends Fragment {
32+
// Sample app rewarded ad unit ID.
33+
public static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/5224354917";
34+
35+
private void startPreload() {
36+
// [START start_preload]
37+
// Define a PreloadConfiguration.
38+
PreloadConfiguration configuration = new PreloadConfiguration.Builder(AD_UNIT_ID).build();
39+
// Start the preloading with a given preload ID, preload configuration.
40+
RewardedAdPreloader.start(AD_UNIT_ID, configuration);
41+
// [END start_preload]
42+
}
43+
44+
private void startPreloadWithCallback() {
45+
// [START start_preload_callback]
46+
// Define a PreloadConfiguration.
47+
PreloadConfiguration configuration = new PreloadConfiguration.Builder(AD_UNIT_ID).build();
48+
49+
// [START set_callback]
50+
// Define a callback to receive preload events.
51+
PreloadCallbackV2 callback =
52+
new PreloadCallbackV2() {
53+
@Override
54+
public void onAdPreloaded(
55+
@NonNull String preloadId, @Nullable ResponseInfo responseInfo) {
56+
// Called when preloaded ads are available.
57+
}
58+
59+
@Override
60+
public void onAdsExhausted(@NonNull String preloadId) {
61+
// Called when no preloaded ads are available.
62+
}
63+
64+
@Override
65+
public void onAdFailedToPreload(@NonNull String preloadId, @NonNull AdError adError) {
66+
// Called when preloaded ads failed to load.
67+
}
68+
};
69+
// [END set_callback]
70+
71+
// Start the preloading with a given preload Id, preload configuration, and callback.
72+
RewardedAdPreloader.start(AD_UNIT_ID, configuration, callback);
73+
// [END start_preload_callback]
74+
}
75+
76+
private void bufferSize() {
77+
// [START set_buffer_size]
78+
// Define a PreloadConfiguration and buffer up to 5 preloaded ads.
79+
PreloadConfiguration configuration =
80+
new PreloadConfiguration.Builder(AD_UNIT_ID).setBufferSize(5).build();
81+
// [END set_buffer_size]
82+
}
83+
84+
private void pollAndShowAd(Activity activity) {
85+
// [START pollAndShowAd]
86+
// pollAd() returns the next available ad and loads another ad in the background.
87+
RewardedAd ad = RewardedAdPreloader.pollAd(AD_UNIT_ID);
88+
89+
if (ad != null) {
90+
// [Optional] Interact with the ad object as needed.
91+
ad.setOnPaidEventListener(
92+
adValue -> {
93+
// [Optional] Send the impression-level ad revenue information to your preferred
94+
// analytics server directly within this callback.
95+
});
96+
97+
// Show the ad immediately.
98+
ad.show(activity, rewardItem -> {});
99+
}
100+
// [END pollAndShowAd]
101+
}
102+
103+
private void isAdAvailable() {
104+
// [START isAdAvailable]
105+
// Verify that a preloaded ad is available.
106+
if (!RewardedAdPreloader.isAdAvailable(AD_UNIT_ID)) {
107+
// No ads are available to show.
108+
}
109+
// [END isAdAvailable]
110+
}
111+
112+
private void numOfAdsAvailable() {
113+
// [START numOfAdsAvailable]
114+
// Check the number of ads available to show.
115+
int numberOfAds = RewardedAdPreloader.getNumAdsAvailable(AD_UNIT_ID);
116+
// [END numOfAdsAvailable]
117+
}
118+
119+
private void getConfiguration() {
120+
// [START getConfiguration]
121+
PreloadConfiguration configuration = RewardedAdPreloader.getConfiguration(AD_UNIT_ID);
122+
// [END getConfiguration]
123+
}
124+
125+
private void destroyAd() {
126+
// [START destroy_ad]
127+
// Stops the preloading and destroy preloaded ads.
128+
RewardedAdPreloader.destroy(AD_UNIT_ID);
129+
// Stops the preloading and destroy all ads.
130+
RewardedAdPreloader.destroyAll();
131+
// [END destroy_ad]
132+
}
133+
}
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)