每按一次按钮,显示Admob插页式广告?

时间:2018-04-18 02:25:11

标签: java android android-studio admob

在这张图片中有8个按钮。现在我在这个应用程序上使用测试广告单元。这个活动我有8个按钮,每个按钮都与目标活动相关联。我的问题是,当我点击此8的任何按钮时,它会显示admob插页式广告并发送到目标活动,但当我关闭目标活动并再次返回main_activity并点击此8个按钮的任何按钮时,admo插页式广告不会显示但在按钮上单击它会打开目标活动。我想在每次点击按钮时显示admob插页式广告。怎么能这样做?

    // Admob Firebase
    AdView myadview = new AdView(this);
    // AdRequest smartBanner
    mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);
    // Admob Intarsital
    MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
}

private void initialize() {
    linear1 = (LinearLayout) findViewById(R.id.linear1);
    linear2 = (LinearLayout) findViewById(R.id.linear2);
    linear3 = (LinearLayout) findViewById(R.id.linear3);
    linear4 = (LinearLayout) findViewById(R.id.linear4);
    button9 = (Button) findViewById(R.id.button9);
    button10 = (Button) findViewById(R.id.button10);
    button11 = (Button) findViewById(R.id.button11);
    button12 = (Button) findViewById(R.id.button12);
    button13 = (Button) findViewById(R.id.button13);
    button14 = (Button) findViewById(R.id.button14);
    button15 = (Button) findViewById(R.id.button15);
    button16 = (Button) findViewById(R.id.button16);
    textview1 = (TextView) findViewById(R.id.textview1);

    button9.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), DhakaActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button10.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), KhulnaActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button11.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), RoungpurActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button12.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), SeyletActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button13.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), ChitagongActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button14.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), BorishalActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button15.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), RajshashiActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });
    button16.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View _v) {
            b.setClass(getApplicationContext(), ChitagongActivity.class);
            startActivity(b);
            if (mInterstitialAd.isLoaded()) {
                mInterstitialAd.show();
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
            mInterstitialAd.setAdListener(new AdListener() {
                @Override
                public void onAdLoaded() {
                    // Code to be executed when an ad finishes loading.
                }

                @Override
                public void onAdFailedToLoad(int errorCode) {
                    // Code to be executed when an ad request fails.
                }

                @Override
                public void onAdOpened() {
                    // Code to be executed when the ad is displayed.
                }

                @Override
                public void onAdLeftApplication() {
                    // Code to be executed when the user has left the app.
                }

                @Override
                public void onAdClosed() {
                    // Code to be executed when when the interstitial ad is closed.
                }
            });
        }
    });
}

image main_activity

1 个答案:

答案 0 :(得分:0)

将加载广告视图的代码部分放在您活动的onResume方法中。因此,每次恢复活动时都会加载新广告。

 // Admob Firebase
AdView myadview = new AdView(this);
// AdRequest smartBanner
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Admob Intarsital
MobileAds.initialize(this,
        "ca-app-pub-3940256099942544~3347511713");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());

将这些放在onResume方法