无法在按钮点击时显示非页内广告

时间:2017-11-06 09:27:01

标签: android admob ads

有10个按钮,活动中的每个按钮导航到新活动。我每次点击按钮时都会编写代码来显示插页式广告。 但是,点击它时,它始终不会显示广告。

我可以知道我在代码中出错了吗?

代码 -

public class home_activity extends AppCompatActivity implements ConnectivityReceiver.ConnectivityReceiverListener {


    private InterstitialAd mInterstitialAd;    
    private static final String TAG = "home_activity";
    private AdView mAdView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("ca-app-pub-4189677300263650/4868306863");
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                mInterstitialAd.loadAd(new AdRequest.Builder().build());
            }
        });

        mAdView = (AdView) findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
        public void motorcycle_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_motorcycle.class));
        }
    }

    public void privatecar_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_car.class));
        }
    }

    public void taxi_upto6_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_motorcycle.class));
        }
    }

    public void bus_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_bus.class));
        }
    }

    public void passauto_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_passauto.class));
        }
    }

    public void goodsauto_public_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, pt_goodsauto_public.class));
        }
    }

    public void goodsauto_private_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, pt_goodsauto_private.class));
        }
    }

    public void commercialvehiclepublic_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_commercialvehiclepublic.class));
        }
    }

    public void commercialvehicleprivate_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, CC_commercialvehicleprivate.class));
        }
    }

    public void agri_btn(View view) {
        //If loaded show ad
        if (mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        } else {
            //Even if Ad is not loaded show the Second Activity
            startActivity(new Intent(home_activity.this, pt_agri.class));
        }
    }
}

P.S - 我试图在项目中实施Banner和插页式广告。 横幅广告工作正常,但在加载活动后需要一些时间才能显示。

2 个答案:

答案 0 :(得分:0)

您显示插页式广告的代码就好了。检查这些事情:

  1. 您是否已初步使用MobileAds。这样做的代码是
  2. MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");

    1. 收听AdLoadFailure。

    2. 如果您刚创建广告单元,可能需要一段时间才能显示实际广告,因此请尝试使用测试广告或注册测试设备。

答案 1 :(得分:0)

要加载插页式广告,您需要使用互联网连接。确保互联网已启用。此外,您应该使用测试APP PUBLISHER ID进行开发。不使用您的生产APP PUBLISHER ID。

对于单独初始化,您可以在按钮单击时将当前按钮ID存储在变量中。并在OnAdClosed()中切换大小写以匹配要打开的特定活动。