【问题标题】:How to show admob interstitial before resuming main activity如何在恢复主要活动之前显示 admob 插页式广告
【发布时间】:2021-04-18 18:40:46
【问题描述】:

我有一个包含 2 个活动的应用。我试图在第二个活动结束时展示一个插页式广告。你能告诉我这段代码是否正确吗?

class secondActivity extends AppCompatActivity {
    private InterstitialAd mInterstitialAd;

    public void loadAd() {
        AdRequest adRequest = new AdRequest.Builder().build();
        InterstitialAd.load(
                this,
                "ca-app-pub-3940256099942544/1033173712",
                adRequest,
                new InterstitialAdLoadCallback() {
                    @Override
                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                        // The mInterstitialAd reference will be null until
                        // an ad is loaded.
                        mInterstitialAd = interstitialAd;
                    }
                });
    }

    private void showInterstitial() {
        // Show the ad if it's ready. Otherwise toast and restart the game.
        if (mInterstitialAd != null) {
            mInterstitialAd.show(this);
        }
    }

    @Override
    public void onBackPressed() {
        showInterstitial();
        super.onBackPressed();
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadAd();
    }
}

【问题讨论】:

    标签: java android admob interstitial


    【解决方案1】:
    @Override
    public void onBackPressed() {
      //Your code here
      super.onBackPressed();
      displayInterstitial();
    }
    
    // Invoke displayInterstitial() when you are ready to display an interstitial.
    public void displayInterstitial() {
      if (interstitial != null) {
        if (interstitial.isLoaded()) {
          interstitial.show();
        }
      }
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      //Call loadad on create
      loadAd();
    }
    
    public void loadAd() {
      interstitial = new InterstitialAd(this);
      interstitial.setAdUnitId(getString(R.string.fullscreeen_ad_id));
      AdRequest adRequest = new AdRequest.Builder().build();
      interstitial.loadAd(adRequest);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-18
      • 1970-01-01
      相关资源
      最近更新 更多