|
| 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 | +} |
0 commit comments