|
| 1 | +package com.google.samples.quickstart.admobexample.java; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.util.Log; |
| 5 | +import android.view.LayoutInflater; |
| 6 | +import android.view.View; |
| 7 | +import android.view.ViewGroup; |
| 8 | +import android.widget.Button; |
| 9 | + |
| 10 | +import androidx.annotation.NonNull; |
| 11 | +import androidx.annotation.Nullable; |
| 12 | +import androidx.annotation.VisibleForTesting; |
| 13 | +import androidx.fragment.app.Fragment; |
| 14 | +import androidx.navigation.fragment.NavHostFragment; |
| 15 | + |
| 16 | +import com.google.android.gms.ads.AdListener; |
| 17 | +import com.google.android.gms.ads.AdRequest; |
| 18 | +import com.google.android.gms.ads.AdView; |
| 19 | +import com.google.android.gms.ads.InterstitialAd; |
| 20 | +import com.google.android.gms.ads.LoadAdError; |
| 21 | +import com.google.android.gms.ads.MobileAds; |
| 22 | +import com.google.samples.quickstart.admobexample.R; |
| 23 | +import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding; |
| 24 | + |
| 25 | +class FirstFragment extends Fragment { |
| 26 | + |
| 27 | + private static final String TAG = "MainActivity"; |
| 28 | + private static final String TEST_APP_ID = "ca-app-pub-3940256099942544~3347511713"; |
| 29 | + |
| 30 | + private AdView mAdView; |
| 31 | + private InterstitialAd mInterstitialAd; |
| 32 | + private Button mLoadInterstitialButton; |
| 33 | + private FragmentFirstBinding binding; |
| 34 | + |
| 35 | + @Nullable |
| 36 | + @Override |
| 37 | + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { |
| 38 | + binding = FragmentFirstBinding.inflate(inflater, container, false); |
| 39 | + return binding.getRoot(); |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 44 | + super.onViewCreated(view, savedInstanceState); |
| 45 | + checkIds(); |
| 46 | + |
| 47 | + // Initialize the Google Mobile Ads SDK |
| 48 | + MobileAds.initialize(getContext()); |
| 49 | + |
| 50 | + mAdView = binding.adView; |
| 51 | + AdRequest adRequest = new AdRequest.Builder().build(); |
| 52 | + mAdView.loadAd(adRequest); |
| 53 | + |
| 54 | + // AdMob ad unit IDs are not currently stored inside the google-services.json file. |
| 55 | + // Developers using AdMob can store them as custom values in a string resource file or |
| 56 | + // simply use constants. Note that the ad units used here are configured to return only test |
| 57 | + // ads, and should not be used outside this sample. |
| 58 | + |
| 59 | + // Create an InterstitialAd object. This same object can be re-used whenever you want to |
| 60 | + // show an interstitial. |
| 61 | + mInterstitialAd = new InterstitialAd(getContext()); |
| 62 | + mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); |
| 63 | + |
| 64 | + mInterstitialAd.setAdListener(new AdListener() { |
| 65 | + @Override |
| 66 | + public void onAdClosed() { |
| 67 | + requestNewInterstitial(); |
| 68 | + beginSecondActivity(); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void onAdLoaded() { |
| 73 | + // Ad received, ready to display |
| 74 | + if (mLoadInterstitialButton != null) { |
| 75 | + mLoadInterstitialButton.setEnabled(true); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public void onAdFailedToLoad(LoadAdError error) { |
| 81 | + Log.w(TAG, "onAdFailedToLoad:" + error.getMessage()); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + mLoadInterstitialButton = binding.loadInterstitialButton; |
| 86 | + mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() { |
| 87 | + @Override |
| 88 | + public void onClick(View v) { |
| 89 | + if (mInterstitialAd.isLoaded()) { |
| 90 | + mInterstitialAd.show(); |
| 91 | + } else { |
| 92 | + beginSecondActivity(); |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + // Disable button if an interstitial ad is not loaded yet. |
| 98 | + mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded()); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Load a new interstitial ad asynchronously. |
| 103 | + */ |
| 104 | + private void requestNewInterstitial() { |
| 105 | + AdRequest adRequest = new AdRequest.Builder().build(); |
| 106 | + |
| 107 | + mInterstitialAd.loadAd(adRequest); |
| 108 | + } |
| 109 | + |
| 110 | + private void beginSecondActivity() { |
| 111 | + NavHostFragment.findNavController(this).navigate(R.id.action_FirstFragment_to_SecondFragment); |
| 112 | + } |
| 113 | + |
| 114 | + /** Called when leaving the activity */ |
| 115 | + @Override |
| 116 | + public void onPause() { |
| 117 | + if (mAdView != null) { |
| 118 | + mAdView.pause(); |
| 119 | + } |
| 120 | + super.onPause(); |
| 121 | + } |
| 122 | + |
| 123 | + /** Called when returning to the activity */ |
| 124 | + @Override |
| 125 | + public void onResume() { |
| 126 | + super.onResume(); |
| 127 | + if (mAdView != null) { |
| 128 | + mAdView.resume(); |
| 129 | + } |
| 130 | + if (!mInterstitialAd.isLoaded()) { |
| 131 | + requestNewInterstitial(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** Called before the activity is destroyed */ |
| 136 | + @Override |
| 137 | + public void onDestroy() { |
| 138 | + if (mAdView != null) { |
| 139 | + mAdView.destroy(); |
| 140 | + } |
| 141 | + super.onDestroy(); |
| 142 | + } |
| 143 | + |
| 144 | + @VisibleForTesting |
| 145 | + public AdView getAdView() { |
| 146 | + return mAdView; |
| 147 | + } |
| 148 | + |
| 149 | + private void checkIds() { |
| 150 | + if (TEST_APP_ID.equals(getString(R.string.admob_app_id))) { |
| 151 | + Log.w(TAG, "Your admob_app_id is not configured correctly, please see the README"); |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments