简体   繁体   中英

I am having trouble creating an Interstitial ad in my android app

I am working on a math type game for android. But i want to create an Interstitial ad. Google has an example of how to create one but it has a button to create the ad. I want my ad to just start at the beginning of the activity and the user just closes it.

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.util.Random;
import android.content.Intent;
import java.util.concurrent.TimeUnit;
import android.os.CountDownTimer;    
import com.google.android.gms.ads.*;

public class activity_2 extends MainActivity implements DialogInterface.OnClickListener{

int getWrong=0, num, sum = 0, score = 0, three, four;

Random rand = new Random();
int one = rand.nextInt(10) + 1;
int two = rand.nextInt(10) + 1;
int solution = one + two;
String myString = String.format("%d + %d =", one, two);

   InterstitialAd mInterstitialAd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);

    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
          //  beginPlayingGame(); * the google sample had a this class which i dont want to work with 
        }
    });


    TextView text = (TextView) findViewById(R.id.Question);
    text.setText("" + myString);


    TextView timeSTART = (TextView) findViewById(R.id.TimmerT);
        timeSTART.setText("10");

requestNewInterstitial();    
    }

private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
              .build();

    mInterstitialAd.loadAd(adRequest);
}

You can not show the add without load it...!

Why you are not using Singleton for that purpose?

You should follow the below steps:

  • First, create a public static InterstitialAd object in your Application(not Activity) class..!

  • Then, you need to initialize your mInterstitialAd object in Application's onCreate method, not in Activity's onCreate....!

  • After that, call the mInterstitialAd object's show method from your Activity class...!

This procedure will allow you to call show Google Add anywhere in your app...!

Your Application class should look like below,

    public class DefaultApplication extends Application {

        public static InterstitialAd mInterstitialAd;
        @Override
        public void onCreate() {
            super.onCreate();

             mInterstitialAd = new InterstitialAd(this);
             mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

             mInterstitialAd.setAdListener(new AdListener() {
                  @Override
                  public void onAdClosed() {
                      requestNewInterstitial();
                     //  beginPlayingGame(); * the google sample had a this class which i dont want to work with 
                  }
             });
        }
private void requestNewInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
              .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
              .build();

    mInterstitialAd.loadAd(adRequest);
}
    }

To load the Add from Activity's onCreate, do like below,

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
    DefaultApplication.mInterstitialAd.show();

}

Firstly, you have to initialize the MobileAds SDK.

So, go to your Application and do what is missing:

MobileAds.initialize(context, getString(R.string.banner_ad_unit_id));

After this, just go to your Activity and show your interstitial ad:

if (interstitialAd != null && interstitialAd.isLoaded()) {
    interstitialAd.show();
}

The Admob example uses a button so that you can readily play with the concept but also to show that you need a specific event on which to show an ad.

You need to find a specific point in your app at which you could display an ad, such as between games/levels.

Displaying an ad when the app starts will interrupt the user's flow, provides a poor user experience and is against Admob policy and will probably get your account banned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM