Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
MyActivity.java
/* * Copyright (C) 2013 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.android.gms.example.interstitialexample; import android.os.Bundle; import android.os.CountDownTimer; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.InterstitialAd; import com.google.android.gms.ads.MobileAds; /** * Main Activity. Inflates main activity xml. */ public class MyActivity extends AppCompatActivity { private static final long GAME_LENGTH_MILLISECONDS = 3000; private InterstitialAd mInterstitialAd; private CountDownTimer mCountDownTimer; private Button mRetryButton; private boolean mGameIsInProgress; private long mTimerMilliseconds; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); // Initialize the Mobile Ads SDK. MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); // Create the InterstitialAd and set the adUnitId. mInterstitialAd = new InterstitialAd(this); // Defined in res/values/strings.xml mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id)); mInterstitialAd.setAdListener(new AdListener() { @Override public void onAdClosed() { startGame(); } }); // Create the "retry" button, which tries to show an interstitial between game plays. mRetryButton = findViewById(R.id.retry_button); mRetryButton.setVisibility(View.INVISIBLE); mRetryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showInterstitial(); } }); startGame(); } private void createTimer(final long milliseconds) { // Create the game timer, which counts down to the end of the level // and shows the "retry" button. if (mCountDownTimer != null) { mCountDownTimer.cancel(); } final TextView textView = findViewById(R.id.timer); mCountDownTimer = new CountDownTimer(milliseconds, 50) { @Override public void onTick(long millisUnitFinished) { mTimerMilliseconds = millisUnitFinished; textView.setText("seconds remaining: " + ((millisUnitFinished / 1000) + 1)); } @Override public void onFinish() { mGameIsInProgress = false; textView.setText("done!"); mRetryButton.setVisibility(View.VISIBLE); } }; } @Override public void onResume() { // Start or resume the game. super.onResume(); if (mGameIsInProgress) { resumeGame(mTimerMilliseconds); } } @Override public void onPause() { // Cancel the timer if the game is paused. mCountDownTimer.cancel(); super.onPause(); } private void showInterstitial() { // Show the ad if it's ready. Otherwise toast and restart the game. if (mInterstitialAd != null && mInterstitialAd.isLoaded()) { mInterstitialAd.show(); } else { Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show(); startGame(); } } private void startGame() { // Request a new ad if one isn't already loaded, hide the button, and kick off the timer. if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) { AdRequest adRequest = new AdRequest.Builder().build(); mInterstitialAd.loadAd(adRequest); } mRetryButton.setVisibility(View.INVISIBLE); resumeGame(GAME_LENGTH_MILLISECONDS); } private void resumeGame(long milliseconds) { // Create a new timer for the correct length and start it. mGameIsInProgress = true; mTimerMilliseconds = milliseconds; createTimer(milliseconds); mCountDownTimer.start(); } }