Android版静态横幅Admob

时间:2013-12-25 18:16:25

标签: admob

我想在我的Android应用程序中放置Admob的静态横幅,但我不知道如何。问题是,当我在应用程序中开始一个新的Activity时,横幅消失,如果我想在新活动中显示横幅,我必须在该活动中创建一个新的横幅。我想知道如何在应用程序的所有活动中显示相同的横幅。

谢谢。

1 个答案:

答案 0 :(得分:-1)

1)创建一个所有Activity类都扩展的MyAdActivity类。

公共类MyAdActivity扩展了Activity {

AdView mAdView=null;

protected void onPause() {
    if (mAdView != null) mAdView.pause();
    super.onPause();
}

protected void onResume() {
    super.onResume();
    if (mAdView != null) mAdView.resume();
}

protected void onDestroy() {
    if (mAdView != null) mAdView.destroy();
    super.onDestroy();
}

public void setUpAndLoadAd(int xmlId, int layoutId, boolean bottom) {

            // The first argument is the name of the XML file for this activity
            // The second argument is the id for the main layout window of the activity
            //    (generally in the xml file named in the first argument)
            // The third argument is just a boolean that says,
            //              "I want the ad to go on the bottom" (true)
            //      ... or  "I want the ad to go on the top" (false)    

    int topOrBottom;

    if (xmlId != 0) setContentView(xmlId); // if you setContentView before
                                                   // you called setUpAndLoadAd, then
                                                   // you can pass a 0 to this method 
                                                   // and it won't get called again

    // Save the pointer to the BannerAdView so we can pause, resume
    // and destroy it with the activity that started it.
    mAdView = new AdView(getBaseContext());
    mAdView.setAdUnitId(getResources().getString(R.string.ad_unit_id));
    mAdView.setAdSize(AdSize.BANNER);

    RelativeLayout layout = (RelativeLayout) findViewById(layoutId);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    if (bottom) topOrBottom = RelativeLayout.ALIGN_PARENT_BOTTOM;
    else topOrBottom = RelativeLayout.ALIGN_PARENT_TOP;

    params.addRule(topOrBottom);
    layout.addView(mAdView, params);

    AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(getResources().getString(R.string.test_device)) // My Galaxy SIII Phone*/
        .build();

    mAdView.loadAd(adRequest);
}

}

然后您的活动将如下所示:

公共类MainActivity扩展了MyAdActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // put the ad on the bottom (if the last argument is false,
    // the ad will go on the top).
    setUpAndLoadAd(R.layout.activity_main, R.id.main_window, true);

    //Any other code you want to put in your activity goes here.
}

}

然后,确保您识别" ad_id_unit" (您从AdMob获得)和" test_device" (在使用设备进行调试时,可以在logcat窗口中找到实际的字符串)。

您需要将元数据行放在清单文件中,但是您不必在那里声明任何特殊的活动,因为您已经列出了扩展MyAdActivity的活动。此外,横幅广告的所有内容都是通过代码完成的,因此您无需以任何方式更改布局xml文件,只是为了确保广告布局的顶部或底部有空间。< / p>

这种广告实施方式并不完全是静态的&#34;广告,但这是一种简单的方法,可以反复使用相同的代码将广告添加到您应用中的多个活动中。