mercredi 22 juin 2016

How can i launch Interstitial Ads from a Menu Item?

The Code from launching an Interstitial Ads through a button is this. I want to launch the Interstitial Ads through a Menu Item. Is it possible to launch a Interstitial Ad through Menu Item. Can somewhere here change the below code and make it Menu Item compatible

Add interstitial ads to an activity

public class MainActivity extends ActionBarActivity {

InterstitialAd mInterstitialAd;
Button mNewGameButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNewGameButton = (Button) findViewById(R.id.newgame_button);

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

    mInterstitialAd.setAdListener(new AdListener() {
        @Override
        public void onAdClosed() {
            requestNewInterstitial();
            beginPlayingGame();
        }
    });

    requestNewInterstitial();

    mNewGameButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
            } else {
                beginPlayingGame();
            }
        }
    });

    beginPlayingGame();
}

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

    mInterstitialAd.loadAd(adRequest);
}

private void beginPlayingGame() {
    // Play for a while, then display the New Game Button
}
 }

Instantiate the InterstitialAd object

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

Display the interstitial

...
  mNewGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
    } else {
        beginPlayingGame();
    }
}
     });
       ...

Creating the AdListener

...
    mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
    requestNewInterstitial();
    beginPlayingGame();
}
      });

... requestNewInterstitial

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

mInterstitialAd.loadAd(adRequest);
}
  ...

I have tried googling but s far i think no one has made a tutorial about launching a Interstitial Ad through Menu Item.

Aucun commentaire:

Enregistrer un commentaire