11package com .google .firebase .example .admob ;
22
3- import android .content .Context ;
43import android .os .Bundle ;
5- import android .util .Log ;
6- import android .view .View ;
7- import android .widget .Button ;
84
95import androidx .appcompat .app .AppCompatActivity ;
106
11- import com .google .android .gms .ads .AdListener ;
12- import com .google .android .gms .ads .AdRequest ;
13- import com .google .android .gms .ads .AdView ;
14- import com .google .android .gms .ads .InterstitialAd ;
15- import com .google .android .gms .ads .LoadAdError ;
167import com .google .android .gms .ads .MobileAds ;
178
18- import devrel .firebase .google .com .firebaseoptions .R ;
19-
209public class MainActivity extends AppCompatActivity {
2110
22- private static final String TAG = "MainActivity" ;
23-
24- private AdView mAdView ;
25- private InterstitialAd mInterstitialAd ;
26- private Button mLoadInterstitialButton ;
27- private Context context = this ;
28-
2911 // [START ads_on_create]
3012 @ Override
3113 protected void onCreate (Bundle savedInstanceState ) {
@@ -35,111 +17,4 @@ protected void onCreate(Bundle savedInstanceState) {
3517 }
3618 // [END ads_on_create]
3719
38- private void loadBannerAd () {
39- // [SNIPPET load_banner_ad]
40- // Load an ad into the AdView.
41- // [START load_banner_ad]
42-
43- // Initialize the Google Mobile Ads SDK
44- MobileAds .initialize (context );
45-
46- AdRequest adRequest = new AdRequest .Builder ().build ();
47- mAdView .loadAd (adRequest );
48- // [END load_banner_ad]
49- }
50-
51- private void initInterstitialAd () {
52- // [START instantiate_interstitial_ad]
53- // Create an InterstitialAd object. This same object can be re-used whenever you want to
54- // show an interstitial.
55- mInterstitialAd = new InterstitialAd (context );
56- mInterstitialAd .setAdUnitId (getString (R .string .interstitial_ad_unit_id ));
57- // [END instantiate_interstitial_ad]
58- }
59-
60- private void createInterstitialAdListener () {
61- // [START create_interstitial_ad_listener]
62- mInterstitialAd .setAdListener (new AdListener () {
63- @ Override
64- public void onAdClosed () {
65- requestNewInterstitial ();
66- beginSecondActivity ();
67- }
68-
69- @ Override
70- public void onAdLoaded () {
71- // Ad received, ready to display
72- // ...
73- }
74-
75- @ Override
76- public void onAdFailedToLoad (LoadAdError loadAdError ) {
77- Log .w (TAG , "onAdFailedToLoad:" + loadAdError .getMessage ());
78- }
79- });
80- // [END create_interstitial_ad_listener]
81- }
82-
83- private void displayInterstitialAd () {
84- // [START display_interstitial_ad]
85- mLoadInterstitialButton = findViewById (R .id .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- // [END display_interstitial_ad]
97- }
98-
99- /**
100- * Load a new interstitial ad asynchronously.
101- */
102- // [START request_new_interstitial]
103- private void requestNewInterstitial () {
104- AdRequest adRequest = new AdRequest .Builder ()
105- .build ();
106-
107- mInterstitialAd .loadAd (adRequest );
108- }
109- // [END request_new_interstitial]
110-
111- private void beginSecondActivity () { }
112-
113- // [START add_lifecycle_methods]
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- // [END add_lifecycle_methods]
144-
14520}
0 commit comments