|
| 1 | +/** |
| 2 | + * Copyright Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.samples.quickstart.admobexample; |
| 17 | + |
| 18 | +import android.content.Intent; |
| 19 | +import android.os.Bundle; |
| 20 | +import android.support.v7.app.AppCompatActivity; |
| 21 | +import android.view.View; |
| 22 | +import android.widget.Button; |
| 23 | +import com.google.android.gms.ads.AdListener; |
| 24 | +// [SNIPPET load_banner_ad] |
| 25 | +// Load an ad into the AdView. |
| 26 | +// [START load_banner_ad] |
| 27 | +import com.google.android.gms.ads.AdRequest; |
| 28 | +import com.google.android.gms.ads.AdView; |
| 29 | +// [START_EXCLUDE] |
| 30 | +import com.google.android.gms.ads.InterstitialAd; |
| 31 | +// [END_EXCLUDE] |
| 32 | + |
| 33 | + |
| 34 | +public class MainActivity extends AppCompatActivity { |
| 35 | + |
| 36 | + private AdView mAdView; |
| 37 | + // [START_EXCLUDE] |
| 38 | + private InterstitialAd mInterstitialAd; |
| 39 | + private Button mLoadInterstitialButton; |
| 40 | + // [END_EXCLUDE] |
| 41 | + |
| 42 | + @Override |
| 43 | + protected void onCreate(Bundle savedInstanceState) { |
| 44 | + super.onCreate(savedInstanceState); |
| 45 | + setContentView(R.layout.activity_main); |
| 46 | + |
| 47 | + mAdView = (AdView) findViewById(R.id.adView); |
| 48 | + AdRequest adRequest = new AdRequest.Builder().build(); |
| 49 | + mAdView.loadAd(adRequest); |
| 50 | + // [END load_banner_ad] |
| 51 | + |
| 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(this); |
| 56 | + mInterstitialAd.setAdUnitId(getString(R.string.test_interstitial_ad_unit_id)); |
| 57 | + // [END instantiate_interstitial_ad] |
| 58 | + |
| 59 | + // [START create_interstitial_ad_listener] |
| 60 | + mInterstitialAd.setAdListener(new AdListener() { |
| 61 | + @Override |
| 62 | + public void onAdClosed() { |
| 63 | + requestNewInterstitial(); |
| 64 | + beginSecondActivity(); |
| 65 | + } |
| 66 | + }); |
| 67 | + // [END create_interstitial_ad_listener] |
| 68 | + |
| 69 | + // [START display_interstitial_ad] |
| 70 | + mLoadInterstitialButton = (Button) findViewById(R.id.load_interstitial_button); |
| 71 | + mLoadInterstitialButton.setOnClickListener(new View.OnClickListener() { |
| 72 | + @Override |
| 73 | + public void onClick(View v) { |
| 74 | + if (mInterstitialAd.isLoaded()) { |
| 75 | + mInterstitialAd.show(); |
| 76 | + } else { |
| 77 | + beginSecondActivity(); |
| 78 | + } |
| 79 | + } |
| 80 | + }); |
| 81 | + // [END display_interstitial_ad] |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Load a new interstitial ad asynchronously. |
| 86 | + */ |
| 87 | + // [START request_new_interstitial] |
| 88 | + private void requestNewInterstitial() { |
| 89 | + AdRequest adRequest = new AdRequest.Builder() |
| 90 | + .build(); |
| 91 | + |
| 92 | + mInterstitialAd.loadAd(adRequest); |
| 93 | + } |
| 94 | + // [END request_new_interstitial] |
| 95 | + |
| 96 | + private void beginSecondActivity() { |
| 97 | + Intent intent = new Intent(this, SecondActivity.class); |
| 98 | + startActivity(intent); |
| 99 | + } |
| 100 | + |
| 101 | + // [START add_lifecycle_methods] |
| 102 | + /** Called when leaving the activity */ |
| 103 | + @Override |
| 104 | + public void onPause() { |
| 105 | + if (mAdView != null) { |
| 106 | + mAdView.pause(); |
| 107 | + } |
| 108 | + super.onPause(); |
| 109 | + } |
| 110 | + |
| 111 | + /** Called when returning to the activity */ |
| 112 | + @Override |
| 113 | + public void onResume() { |
| 114 | + super.onResume(); |
| 115 | + if (mAdView != null) { |
| 116 | + mAdView.resume(); |
| 117 | + } |
| 118 | + if (!mInterstitialAd.isLoaded()) { |
| 119 | + requestNewInterstitial(); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + /** Called before the activity is destroyed */ |
| 124 | + @Override |
| 125 | + public void onDestroy() { |
| 126 | + if (mAdView != null) { |
| 127 | + mAdView.destroy(); |
| 128 | + } |
| 129 | + super.onDestroy(); |
| 130 | + } |
| 131 | + // [END add_lifecycle_methods] |
| 132 | +} |
0 commit comments