コード例 #1
0
  public void onFinishFetchAd() {
    MoPubView view = mMoPubViewReference.get();
    if (view != null) {
      view.removeAllViews();
      mAdView.setVisibility(View.VISIBLE);
      FrameLayout.LayoutParams layoutParams =
          new FrameLayout.LayoutParams(
              FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
      layoutParams.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL;
      view.addView(mAdView, layoutParams);

      view.adLoaded();
    }
  }
コード例 #2
0
  public void loadAd() {
    MoPubView view = mMoPubViewReference.get();
    if (view == null) {
      return;
    }

    mAdView = new GoogleAdView(view.getContext());

    // The following parameters are required.  Fail if they aren't set
    JSONObject object;
    String pubId;
    String companyName;
    String appName;
    try {
      object = (JSONObject) new JSONTokener(mParams).nextValue();
      pubId = object.getString("Gclientid");
      companyName = object.getString("Gcompanyname");
      appName = object.getString("Gappname");
    } catch (JSONException e) {
      view.adFailed();
      return;
    }

    // The rest of the parameters are optional
    AdSenseSpec.AdType adtype = AdType.TEXT_IMAGE;
    Boolean testState = false;
    String keywords = "";
    String channelId = "";
    try {
      String at = object.getString("Gadtype");
      if (at.equals("GADAdSenseTextAdType")) {
        adtype = AdType.TEXT;
      } else if (at.equals("GADAdSenseImageAdType")) {
        adtype = AdType.IMAGE;
      }
    } catch (JSONException e) {
    }
    try {
      testState = object.getString("Gtestadrequest").equals("1");
    } catch (JSONException e) {
    }
    try {
      keywords = object.getString("Gkeywords");
    } catch (JSONException e) {
    }
    try {
      channelId = object.getString("Gchannelids");
    } catch (JSONException e) {
    }

    if (keywords == null || keywords.equals("")) {
      keywords = "None";
    }

    AdSenseSpec adSenseSpec =
        new AdSenseSpec(pubId) // Specify client ID. (Required)
            .setCompanyName(companyName) // Set company name. (Required)
            .setAppName(appName) // Set application name. (Required)
            .setKeywords(keywords) // Specify keywords.
            .setChannel(channelId) // Set channel ID.
            .setAdType(adtype) // Set ad type to Text.
            // .setExpandDirection(AdSenseSpec.ExpandDirection.TOP)
            .setAdTestEnabled(testState); // Keep

    if (view.getAdWidth() == 300 && view.getAdHeight() == 250) {
      adSenseSpec.setAdFormat(AdFormat.FORMAT_300x250);
    } else {
      adSenseSpec.setAdFormat(AdFormat.FORMAT_320x50);
    }

    mAdView.setAdViewListener(this);
    Log.d("MoPub", "Showing AdSense ad...");

    // The GoogleAdView has to be in the view hierarchy to make a request
    mAdView.setVisibility(View.INVISIBLE);
    view.addView(
        mAdView,
        new FrameLayout.LayoutParams(
            FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT));

    mAdView.showAds(adSenseSpec);
  }