11package com .ahmadabuhasan .qrbarcode ;
22
3+ import android .annotation .SuppressLint ;
34import android .content .Context ;
4- import android .os . Handler ;
5+ import android .util . Log ;
56
6- import com .google .android .gms .ads .AdListener ;
7+ import androidx .annotation .NonNull ;
8+
9+ import com .google .android .gms .ads .AdError ;
710import com .google .android .gms .ads .AdRequest ;
8- import com .google .android .gms .ads .InterstitialAd ;
11+ import com .google .android .gms .ads .FullScreenContentCallback ;
12+ import com .google .android .gms .ads .LoadAdError ;
13+ import com .google .android .gms .ads .interstitial .InterstitialAd ;
14+ import com .google .android .gms .ads .interstitial .InterstitialAdLoadCallback ;
915
1016public class Utils {
1117
18+ private static final String TAG = "Utils" ;
19+ private static final String AD_UNIT_ID = "ca-app-pub-8638037215789792/1721440279" ;
1220 public static InterstitialAd interstitialAd ;
1321
14- public void interstitialAdsShow (Context context ) {
15- interstitialAd = new InterstitialAd (context );
16- interstitialAd .setAdUnitId (context .getString (R .string .AdMob_Interstitial_Ads_ID ));
17- interstitialAd .loadAd (new AdRequest .Builder ().build ());
18- interstitialAd .setAdListener (new AdListener () {
19- @ Override
20- public void onAdLoaded () {
21- if (interstitialAd .isLoaded ()) {
22- interstitialAd .show ();
23- }
24- }
25- });
22+ public void loadAd (Context context ) {
23+ AdRequest adRequest = new AdRequest .Builder ().build ();
24+ InterstitialAd .load (
25+ context ,
26+ AD_UNIT_ID ,
27+ adRequest ,
28+ new InterstitialAdLoadCallback () {
29+ @ Override
30+ public void onAdLoaded (@ NonNull InterstitialAd mInterstitialAd ) {
31+ // The mInterstitialAd reference will be null until
32+ // an ad is loaded.
33+ interstitialAd = mInterstitialAd ;
34+ Log .i (TAG , "onAdLoaded" );
35+ interstitialAd .setFullScreenContentCallback (
36+ new FullScreenContentCallback () {
37+ @ Override
38+ public void onAdDismissedFullScreenContent () {
39+ // Called when fullscreen content is dismissed.
40+ // Make sure to set your reference to null so you don't
41+ // show it a second time.
42+ interstitialAd = null ;
43+ Log .d ("TAG" , "The ad was dismissed." );
44+ }
45+
46+ @ Override
47+ public void onAdFailedToShowFullScreenContent (@ NonNull AdError adError ) {
48+ // Called when fullscreen content failed to show.
49+ // Make sure to set your reference to null so you don't
50+ // show it a second time.
51+ interstitialAd = null ;
52+ Log .d ("TAG" , "The ad failed to show." );
53+ }
54+
55+ @ Override
56+ public void onAdShowedFullScreenContent () {
57+ // Called when fullscreen content is shown.
58+ Log .d ("TAG" , "The ad was shown." );
59+ }
60+ });
61+ }
62+
63+ @ Override
64+ public void onAdFailedToLoad (@ NonNull LoadAdError loadAdError ) {
65+ // Handle the error
66+ Log .i (TAG , loadAdError .getMessage ());
67+ interstitialAd = null ;
68+
69+ @ SuppressLint ("DefaultLocale" ) String error =
70+ String .format (
71+ "domain: %s, code: %d, message: %s" ,
72+ loadAdError .getDomain (), loadAdError .getCode (), loadAdError .getMessage ());
73+ Log .d (TAG , "onAdFailedToLoad() with error: " + error );
74+ }
75+ });
2676 }
2777}
0 commit comments