Skip to content

Commit 843e0ad

Browse files
thatfiredevdpebot
andauthored
refactor(admob): upgrade quickstart to admob 20.0.0 (#1277)
* Auto-update dependencies. * refactor(admob): upgrade quickstart to admob 20.0.0 * test(admob): delete tests from AdMob v19 Co-authored-by: DPE bot <[email protected]>
1 parent cf8d0ee commit 843e0ad

File tree

34 files changed

+154
-268
lines changed

34 files changed

+154
-268
lines changed

admob/app/build.gradle

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ dependencies {
3838
implementation 'androidx.appcompat:appcompat:1.2.0'
3939
implementation 'com.google.android.material:material:1.3.0'
4040
implementation 'androidx.browser:browser:1.0.0'
41-
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'
42-
implementation 'androidx.navigation:navigation-ui-ktx:2.3.4'
41+
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
42+
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
4343

44-
implementation 'com.google.android.gms:play-services-ads:19.7.0'
44+
implementation 'com.google.android.gms:play-services-ads:20.0.0'
4545

4646
// Import the Firebase BoM (see: https://firebase.google.com/docs/android/learn-more#bom)
47-
implementation platform('com.google.firebase:firebase-bom:26.6.0')
47+
implementation platform('com.google.firebase:firebase-bom:27.0.0')
4848

4949
// For an optimal experience using AdMob, add the Firebase SDK
5050
// for Google Analytics. This is recommended, but not required.
5151
implementation 'com.google.firebase:firebase-analytics'
5252

53-
debugImplementation "androidx.fragment:fragment-testing:1.3.1"
53+
debugImplementation "androidx.fragment:fragment-testing:1.3.2"
5454
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
5555
androidTestImplementation 'androidx.test:rules:1.3.0'
5656
androidTestImplementation 'androidx.test:runner:1.3.0'

admob/app/src/androidTest/java/com/google/samples/quickstart/admobexample/AdViewIdlingResource.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

admob/app/src/androidTest/java/com/google/samples/quickstart/admobexample/InterstitialAdTest.java

Lines changed: 0 additions & 51 deletions
This file was deleted.

admob/app/src/main/java/com/google/samples/quickstart/admobexample/java/FirstFragment.java

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
import androidx.fragment.app.Fragment;
1414
import androidx.navigation.fragment.NavHostFragment;
1515

16-
import com.google.android.gms.ads.AdListener;
1716
import com.google.android.gms.ads.AdRequest;
1817
import com.google.android.gms.ads.AdView;
19-
import com.google.android.gms.ads.InterstitialAd;
18+
import com.google.android.gms.ads.FullScreenContentCallback;
2019
import com.google.android.gms.ads.LoadAdError;
2120
import com.google.android.gms.ads.MobileAds;
21+
import com.google.android.gms.ads.interstitial.InterstitialAd;
22+
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
2223
import com.google.samples.quickstart.admobexample.R;
2324
import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding;
2425

@@ -48,63 +49,64 @@ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceStat
4849
MobileAds.initialize(getContext());
4950

5051
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));
6352

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-
});
53+
requestNewInterstitial();
8454

8555
mLoadInterstitialButton = binding.loadInterstitialButton;
8656
mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() {
8757
@Override
8858
public void onClick(View v) {
89-
if (mInterstitialAd.isLoaded()) {
90-
mInterstitialAd.show();
59+
if (mInterstitialAd != null) {
60+
mInterstitialAd.show(getActivity());
9161
} else {
9262
beginSecondActivity();
9363
}
9464
}
9565
});
9666

9767
// Disable button if an interstitial ad is not loaded yet.
98-
mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded());
68+
mLoadInterstitialButton.setEnabled(mInterstitialAd != null);
9969
}
10070

10171
/**
10272
* Load a new interstitial ad asynchronously.
10373
*/
10474
private void requestNewInterstitial() {
10575
AdRequest adRequest = new AdRequest.Builder().build();
76+
mAdView.loadAd(adRequest);
77+
78+
// AdMob ad unit IDs are not currently stored inside the google-services.json file.
79+
// Developers using AdMob can store them as custom values in a string resource file or
80+
// simply use constants. Note that the ad units used here are configured to return only test
81+
// ads, and should not be used outside this sample.
82+
InterstitialAd.load(getContext(), getString(R.string.interstitial_ad_unit_id), adRequest, new InterstitialAdLoadCallback() {
83+
@Override
84+
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
85+
super.onAdLoaded(interstitialAd);
86+
mInterstitialAd = interstitialAd;
10687

107-
mInterstitialAd.loadAd(adRequest);
88+
// Ad received, ready to display
89+
if (mLoadInterstitialButton != null) {
90+
mLoadInterstitialButton.setEnabled(true);
91+
}
92+
93+
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
94+
@Override
95+
public void onAdDismissedFullScreenContent() {
96+
super.onAdDismissedFullScreenContent();
97+
requestNewInterstitial();
98+
beginSecondActivity();
99+
}
100+
});
101+
}
102+
103+
@Override
104+
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
105+
super.onAdFailedToLoad(loadAdError);
106+
mInterstitialAd = null;
107+
Log.w(TAG, "onAdFailedToLoad:" + loadAdError.getMessage());
108+
}
109+
});
108110
}
109111

110112
private void beginSecondActivity() {
@@ -127,7 +129,7 @@ public void onResume() {
127129
if (mAdView != null) {
128130
mAdView.resume();
129131
}
130-
if (!mInterstitialAd.isLoaded()) {
132+
if (mInterstitialAd == null) {
131133
requestNewInterstitial();
132134
}
133135
}

0 commit comments

Comments
 (0)