Skip to content

Commit 2829f7c

Browse files
Nicholas Ventimigliacopybara-github
authored andcommitted
Updated snippets.
PiperOrigin-RevId: 904133837
1 parent d45feba commit 2829f7c

2 files changed

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

0 commit comments

Comments
 (0)