如何制作Android插页式广告?

Moh*_*ead 11 android admob

我从许多博客尝试过的东西,但没有一个给出一步一步的解决方案.我应该在AdMob网站上编辑某些内容吗?我是通过"网站和应用"标签下的广告位置/应用选项创建的.

我用过这段代码:

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);
Run Code Online (Sandbox Code Playgroud)

Mik*_*yer 12

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

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);
  }
}
Run Code Online (Sandbox Code Playgroud)


Eri*_*lag 11

与横幅不同,插入式广告一旦加载就不会自动显示.你必须听取AdMob的onReceiveAd()回调,并在回调中,打电话interstital.show()来显示你的插页式广告.

public YourActivity extends Activity implements AdListener {
  ...

  @Override
  public void onReceiveAd(Ad ad) {
    Log.d("OK", "Received ad");
    if (ad == interstitial) {
      interstitial.show();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在这里查看代码示例.此示例将在收到插页式广告后立即显示插页式广告.或者,您可能希望等到适当的时间来显示插页式广告,例如在游戏级别结束时,您可以检查interstitial.isReady()是否可以显示插页式广告.


小智 6

你不能再实现AdListener,我用这种方式:

final InterstitialAd mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        mInterstitialAd.show();
    }
});
mInterstitialAd.loadAd(adRequestInter);
Run Code Online (Sandbox Code Playgroud)

将您自己的ID放在名为interstitial_ad_unit_id的strings.xml中,或替换

getResources().getString(R.string.interstitial_ad_unit_id)
Run Code Online (Sandbox Code Playgroud)

与您的ID.