Banner广告集成说明

横幅(Banner)

1. 确定AdHubSDK的主SDK文件已经拷贝到工程文件的libs子目录下参考

2. 在需要添加Banner广告的Activity对应的布局文件中加入一个AdView[com.hubcloud.adhubsdk.AdView]控件来给样式定位

  • 例如:
<!-- view for AdHub Banner Ad -->
    <com.hubcloud.adhubsdk.AdView
        android:id="@+id/adView" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adUnitId="@string/banner_ad_unit_id" />

3. 添加广告代码,添加广告代码前请确定已初始化SDK

  • 例如:
    final AdView adView = (AdView) findViewById(R.id.adView);
    adView.setAdUnitId(adUnitId);
    final AdRequest adRequest = new AdRequest.Builder().build();
    //建议使用此方法调用loadAd方法
    adView.post(new Runnable() {
            @Override
            public void run() {
                adView.loadAd(adRequest);
            }
        });
  • 构造AdRequest:
    AdRequest adRequest = new AdRequest.Builder().build();
  • 属性设置: adView.setAdUnitId(adUnitId); // 设置广告位ID adView.setAdListener(adListener); // 设置事件监听器

  • 控制接口: adView.loadAd(adRequest); // 加载广告

  • AdLisenter接口定义:

// A listener for receiving notifications during the lifecycle of an ad.
public abstract class AdListener {

    // Constructor
    protected AdListener() {
    }

    // Called when an ad is received.
    public void onAdLoaded() {
    }

    // Called when an ad become visible.
    public void onAdShown() {
    }

    // Called when an ad request logFailed. The error code is usually ERROR_CODE_INTERNAL_ERROR,
    // ERROR_CODE_INVALID_REQUEST, ERROR_CODE_NETWORK_ERROR, or ERROR_CODE_NO_FILL.
    public void onAdFailedToLoad(int errorCode) {
    }

    // Called when an ad leaves the application (e.g., to go to the browser).
    public void onAdLeftApplication() {
    }

    // Called when the user is about to return to the application after clicking on an ad.
    public void onAdClosed() {
    }

    // Called when an ad opens an overlay that covers the screen.
    public void onAdOpened() {
    }

    // Called when an ad is clicked.  The current activity will be
    // paused as the user switches to the activity launched from the
    // ad interaction.  For example, the user may click a link that
    // opens a web browser, or touch a click-to-call link which
    // launches the phone dialer.
    public void onAdClicked() {
    }
}

PS:更多设置请参考Demo中的com.hubcloud.adhubsdkdemo.BannerActivity的代码.

results matching ""

    No results matching ""