30 秒后显示插页式广告 Audience Network Facebook

Show interstitialAd after 30 seconds Audience Network Facebook

我想为我的 Android 应用程序使用 Facebook Audience Network,我在应用程序内集成了 interstitialAd,它运行良好,但显示为 Activity 启动。我想在 30 秒后显示广告。这是我的代码

 interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");

    interstitialAd.setAdListener(new InterstitialAdListener() {
      

        @Override
        public void onAdLoaded(Ad ad) {
            // Interstitial ad is loaded and ready to be displayed
            Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
            // Show the ad
            interstitialAd.show();
        }

       
    });

    interstitialAd.loadAd();

我试过用这个来安排我的广告,但没有成功。

private void showAdWithDelay() {
   Handler handler = new Handler();
   handler.postDelayed(new Runnable() {
       public void run() {
            // Check if interstitialAd has been loaded successfully
           if(interstitialAd == null || !interstitialAd.isAdLoaded()) {
               return;
           }
            // Check if the ad is already expired or invalidated, and do not show ad if that is the case. You will not get paid to show an invalidated ad.
           if(interstitialAd.isAdInvalidated()) {
               return;
           }
           // Show the ad
            interstitialAd.show(); 
       }
   }, 3000); 
}

我更喜欢这样做:

      Handler handler = new Handler();
      interstitialAd = new InterstitialAd(this, "XXX");
      interstitialAd.setAdListener(new InterstitialAdListener() {
        @Override
        public void onAdLoaded(Ad ad) {
          handler.postDelayed(new Runnable() {
            @Override
            public void run() {
              interstitialAd.show();
            }
          }, YOUR_TIME);
        }
      });

      interstitialAd.loadAd();

如果您想更精确,请执行此操作:

      final long lastTime = System.currentTimeMillis();
      Handler handler = new Handler();
      interstitialAd = new InterstitialAd(this, "XXX");
      interstitialAd.setAdListener(new InterstitialAdListener() {
        @Override
        public void onAdLoaded(Ad ad) {
          long now = System.currentTimeMillis();
          long timeWait;
          if (now - lastTime >= YOUR_TIME){
            timeWait = 0;
          }else {
            timeWait = YOUR_TIME-(now-lastTime);
          }
          handler.postDelayed(new Runnable() {
            @Override
            public void run() {
              interstitialAd.show();
            }
          }, timeWait);
        }
      });

      interstitialAd.loadAd();

我认为您的实施违反了 AdMob 政策,可能会给您带来麻烦。

查看此页面以获取有关 right/wrong 插页式广告实施的帮助(特别是“意外启动的插页式广告”点):https://support.google.com/admob/answer/6201362?hl=en&ref_topic=2936214

为了遵守 AdMob 政策,我建议您只在 activity/app 自然过渡中显示插页式广告,不同的实施方式可能会导致您被 Admob 禁止。

为此,您可以将代码更改为首先仅加载广告并跟踪其加载状态,如下所示:

boolean isAdLoaded = false;

interstitialAd = new InterstitialAd(this, "1555910157949688_1556058954601475");

interstitialAd.setAdListener(new InterstitialAdListener() 
{ 
    @Override
    public void onAdLoaded(Ad ad) 
    {
        // Interstitial ad is loaded and ready to be displayed
        Log.d(TAG, "Interstitial ad is loaded and ready to be displayed!");
        // The ad has been loaded
        isAdLoaded = true;
    }
});

interstitialAd.loadAd();

然后在应用程序的自然过渡中显示它,例如当用户点击一个按钮转到不同的 activity:

shareButton.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        //Show interstitial if it is loaded
        if(isAdLoaded)
            interstitialAd.show();
        
        //Open Activity
        startActivity(new Intent(this,OtherActivity.class));
    }
});