当按下按钮时,绝对广告不会显示

时间:2018-05-02 08:34:09

标签: android android-studio admob

大家好,我想在用户按下按钮时显示空白广告 这是我在activity.xml中的代码:

 <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxx">
    </com.google.android.gms.ads.AdView>

,并在类activity.java中:

@Override
public void onBackPressed()
{
    InterstitialAd mInterstitialAd;
    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxx");
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxxxx");
    if (mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                super.onAdClosed();
                finish();
            }
        });
    }else{
        super.onBackPressed();
    }

}

3 个答案:

答案 0 :(得分:0)

我可以看到您正在使用const AuthenticationController = require('./controller/AuthenticationController') const AuthenticationControllerPolicy = require('./policies/AuthenticationControllerPolicy') module.exports = (app) => { app.post('/register', AuthenticationControllerPolicy.register, AuthenticationController.register) app.post('/login', AuthenticationController.login) } 方法初始化和加载广告。所以,每次它都没有立即加载。因此,在//Array 1 $arr1 = array('A', 'B', 'C', 'D', 'A', 'E'); // Same element will be sort out in pair //Array 2 $arr2 = array('X', 'Y', 'Z' , 'X', 'Y', 'X'); // Same element will be sort out in pair $output = array(); $used_pair = array(); // Keeping used pair $i = 1; foreach ($arr1 as $each1) { foreach ($arr2 as $each2) { if (!in_array($each1.$each2, $used_pair)) { $output[] = array($each1, $each2); $used_pair[] = $each1 . $each2; } } } print_r($used_pair); // array with pair string print_r($output); // array with pair array onBackPressed()初始化并加载广告,检查广告是否已加载,然后展示您的广告。

onCreate()内,请执行以下操作:

onBackPressed()

onCreate()里面的内容是这样的:

protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    ...
    InterstitialAd mInterstitialAd;
    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxx");
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxxxx");
    mInterstitialAd.loadAd(new AdRequest.Builder().build());
}

此外,插页式广告不需要xml元素,因此请从xml中删除AdView。

答案 1 :(得分:0)

您必须在onCreate内初始化admob,然后在设置addunit Id后首先加载add,调用mInterstitialAd.loadAd(new AdRequest.Builder().build());方法 如果您只想显示插页式广告

,也无需在xml中添加AdView

答案 2 :(得分:0)

onCreate()

的变化
InterstitialAd mInterstitialAd;
    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxx");
    mInterstitialAd = new InterstitialAd(this);
    mInterstitialAd.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxxxx");
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.
        finish();
    }
});
    mInterstitialAd.loadAd(new AdRequest.Builder().build());

根据onBackPressed()中的建议更改您的代码:

if (mInterstitialAd.isLoaded()) {
    mInterstitialAd.show();

}else{
    super.onBackPressed();
}

并且InterstitialAd不需要xml视图,因此请删除