Skip to content

refactor(admob): upgrade quickstart to admob 20.0.0 #1277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions admob/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.3.0'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.4'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.4'
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'

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

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

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

debugImplementation "androidx.fragment:fragment-testing:1.3.1"
debugImplementation "androidx.fragment:fragment-testing:1.3.2"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
androidTestImplementation 'androidx.test:rules:1.3.0'
androidTestImplementation 'androidx.test:runner:1.3.0'
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
import androidx.fragment.app.Fragment;
import androidx.navigation.fragment.NavHostFragment;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.samples.quickstart.admobexample.R;
import com.google.samples.quickstart.admobexample.databinding.FragmentFirstBinding;

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

mAdView = binding.adView;
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// AdMob ad unit IDs are not currently stored inside the google-services.json file.
// Developers using AdMob can store them as custom values in a string resource file or
// simply use constants. Note that the ad units used here are configured to return only test
// ads, and should not be used outside this sample.

// Create an InterstitialAd object. This same object can be re-used whenever you want to
// show an interstitial.
mInterstitialAd = new InterstitialAd(getContext());
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
beginSecondActivity();
}

@Override
public void onAdLoaded() {
// Ad received, ready to display
if (mLoadInterstitialButton != null) {
mLoadInterstitialButton.setEnabled(true);
}
}

@Override
public void onAdFailedToLoad(LoadAdError error) {
Log.w(TAG, "onAdFailedToLoad:" + error.getMessage());
}
});
requestNewInterstitial();

mLoadInterstitialButton = binding.loadInterstitialButton;
mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
if (mInterstitialAd != null) {
mInterstitialAd.show(getActivity());
} else {
beginSecondActivity();
}
}
});

// Disable button if an interstitial ad is not loaded yet.
mLoadInterstitialButton.setEnabled(mInterstitialAd.isLoaded());
mLoadInterstitialButton.setEnabled(mInterstitialAd != null);
}

/**
* Load a new interstitial ad asynchronously.
*/
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// AdMob ad unit IDs are not currently stored inside the google-services.json file.
// Developers using AdMob can store them as custom values in a string resource file or
// simply use constants. Note that the ad units used here are configured to return only test
// ads, and should not be used outside this sample.
InterstitialAd.load(getContext(), getString(R.string.interstitial_ad_unit_id), adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
super.onAdLoaded(interstitialAd);
mInterstitialAd = interstitialAd;

mInterstitialAd.loadAd(adRequest);
// Ad received, ready to display
if (mLoadInterstitialButton != null) {
mLoadInterstitialButton.setEnabled(true);
}

mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
super.onAdDismissedFullScreenContent();
requestNewInterstitial();
beginSecondActivity();
}
});
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
mInterstitialAd = null;
Log.w(TAG, "onAdFailedToLoad:" + loadAdError.getMessage());
}
});
}

private void beginSecondActivity() {
Expand All @@ -127,7 +129,7 @@ public void onResume() {
if (mAdView != null) {
mAdView.resume();
}
if (!mInterstitialAd.isLoaded()) {
if (mInterstitialAd == null) {
requestNewInterstitial();
}
}
Expand Down
Loading