如何制作 Android 插页式广告?

How to create Android interstitial ads?(如何制作 Android 插页式广告?)
本文介绍了如何制作 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 插页式广告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)