如何制作Android非页内广告? [英] How to create Android interstitial ads?

查看:91
本文介绍了如何制作Android非页内广告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了许多博客的内容,但是没有一个提供逐步解决方案。我应该在AdMob网站上进行修改吗?我是通过网站和网站下的广告坐在/应用选项创建的网站。应用标签。

I tried things from many blogs but none gave a step-by-step solution. Should I edit something on the AdMob site? I created the site from the ad sit/app option under the Sites & Apps tab.

我使用了以下代码:

interstitial = new InterstitialAd(this, "MyAdMobID");
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
// Create ad request
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
// Begin loading your interstitial      
interstitial.loadAd(adRequest);
adRequest.setTesting(true);


推荐答案

使用最后一个Android框架,我发现每次广告关闭时,都需要调用load()函数。

Using the last Android framework, I figured out that I need to call the load() function each time the ad is closed.

import com.google.android.gms.ads.*;
import android.os.Handler;
import android.os.Looper;
import android.app.Activity;

class MyActivity extends Activity implements AdListener {
  private InterstitialAd adView;  // The ad
  private Handler mHandler;       // Handler to display the ad on the UI thread
  private Runnable displayAd;     // Code to execute to perform this operation

  @Override
  public void onCreate(Bundle savedInstanceState) {
    adView = new InterstitialAd(mContext);
    adView.setAdUnitId("ca-app-pub-XXXXXXXXXX");
    adView.setAdListener(this);
    mHandler = new Handler(Looper.getMainLooper());
    displayAd = new Runnable() {
      public void run() {  
        runOnUiThread(new Runnable() { 
          public void run() { 
            if (adView.isLoaded()) {
              adView.show();
            }
          }
        });
      }
    };
    loadAd();
  }

  @Override
  public void onAdClosed() {
    loadAd(); // Need to reload the Ad when it is closed.
  }

  void loadAd() {
    AdRequest adRequest = new AdRequest.Builder()
    //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
   .build();

    // Load the adView object witht he request
    adView.loadAd(adRequest);
  }

  //Call displayInterstitial() once you are ready to display the ad.
  public void displayInterstitial() {
    mHandler.postDelayed(displayAd, 1);
  }
}

这篇关于如何制作Android非页内广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆