Java源码示例:com.facebook.ads.AdView

示例1
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initialize status label
    statusLabel = getString(R.string.loading_status);

    // Create a banner's ad view with a unique placement ID (generate your own on the Facebook app settings).
    // Use different ID for each ad placement in your app.
    boolean isTablet = getResources().getBoolean(R.bool.is_tablet);
    adViewBanner = new AdView(this.getActivity(), "YOUR_PLACEMENT_ID",
            isTablet ? AdSize.BANNER_HEIGHT_90 : AdSize.BANNER_HEIGHT_50);

    // Set a listener to get notified on changes or when the user interact with the ad.
    adViewBanner.setAdListener(this);

    // Initiate a request to load an ad.
    adViewBanner.loadAd();
}
 
示例2
@Override
public View requestAd(MediatedBannerAdViewController mBC, Activity activity, String parameter, String uid, int width, int height, TargetingParameters tp) {
    FacebookListener fbListener = new FacebookListener(mBC, this.getClass().getSimpleName());
    AdSize adSize;
    if (width == 320 && height == 50) {
        adSize = AdSize.BANNER_320_50;
    } else if (height == 50) {
        adSize = AdSize.BANNER_HEIGHT_50;
    } else if (height == 90) {
        adSize = AdSize.BANNER_HEIGHT_90;
    } else if (height == 250) {
        adSize = AdSize.RECTANGLE_HEIGHT_250;
    } else {
        Clog.e(Clog.mediationLogTag, "Facebook - Attempted to instantiate with size other than the allowed size of 320x50, -1x50, -1x90, -1x250");
        mBC.onAdFailed(ResultCode.UNABLE_TO_FILL);
        return null;
    }
    adView = new AdView(activity, uid, adSize);
    adView.setAdListener(fbListener);
    adView.loadAd();
    return adView;
}
 
示例3
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Initialize status label
    statusLabel = getString(R.string.loading_status);

    adViewRectangle = new AdView(this.getActivity(), "YOUR_PLACEMENT_ID", AdSize.RECTANGLE_HEIGHT_250);

    // Set a listener to get notified on changes or when the user interact with the ad.
    adViewRectangle.setAdListener(this);

    // Initiate a request to load an ad.
    adViewRectangle.loadAd();
}
 
示例4
@Override
public void requestBannerAd(final Context context,
    MediationBannerListener listener,
    Bundle serverParameters,
    final AdSize adSize,
    final MediationAdRequest adRequest,
    Bundle mediationExtras) {
  mBannerListener = listener;

  final String placementID = getPlacementID(serverParameters);
  if (TextUtils.isEmpty(placementID)) {
    Log.e(TAG, createAdapterError(ERROR_INVALID_SERVER_PARAMETERS,
        "Failed to request ad: placementID is null or empty."));
    mBannerListener.onAdFailedToLoad(this, ERROR_INVALID_SERVER_PARAMETERS);
    return;
  }

  final com.facebook.ads.AdSize facebookAdSize = getAdSize(context, adSize);
  if (facebookAdSize == null) {
    Log.w(TAG, createAdapterError(ERROR_BANNER_SIZE_MISMATCH,
        "There is no matching Facebook ad size for Google ad size: " + adSize.toString()));
    mBannerListener.onAdFailedToLoad(this, ERROR_BANNER_SIZE_MISMATCH);
    return;
  }

  FacebookInitializer.getInstance()
      .initialize(
          context,
          placementID,
          new FacebookInitializer.Listener() {
            @Override
            public void onInitializeSuccess() {
              mAdView = new AdView(context, placementID, facebookAdSize);
              buildAdRequest(adRequest);

              FrameLayout.LayoutParams adViewLayoutParams = new FrameLayout.LayoutParams(
                  adSize.getWidthInPixels(context), LayoutParams.WRAP_CONTENT);
              mWrappedAdView = new FrameLayout(context);
              mAdView.setLayoutParams(adViewLayoutParams);
              mWrappedAdView.addView(mAdView);
              mAdView.loadAd(
                  mAdView.buildLoadAdConfig()
                      .withAdListener(new BannerListener())
                      .build()
              );
            }

            @Override
            public void onInitializeError(String message) {
              Log.w(TAG, createAdapterError(ERROR_FACEBOOK_INITIALIZATION, message));
              if (mBannerListener != null) {
                mBannerListener.onAdFailedToLoad(FacebookAdapter.this,
                    ERROR_FACEBOOK_INITIALIZATION);
              }
            }
          });
}