Handle AdWhirl onFailure

送分小仙女□ 提交于 2019-12-01 06:44:48

OK I've figured this out now. There are two possible ways, where one is very easy and the other requires a little bit more work.

The easy way

The adwhirl layout stays invisible as long as it has nothing to show. So you can simply create a FrameLayout containing your fallback view in the background and the adwhirl view in the front similar to this:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="53dp"
        android:layout_gravity="center_horizontal"
        >
    <!-- fallback view -->
    <TextView
        android:id="@+id/ad_fallback"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="nothing to say..."
        >
</FrameLayout>

In your code you can then add the view to the layout (where parentView is the inflated layout shown above):

final DisplayMetrics dm = activity.getResources().getDisplayMetrics();
final AdWhirlLayout adView = new AdWhirlLayout(activity, ADWHIRL_ID);
adView.setMaxWidth((int) (dm.density * 320));
adView.setMaxHeight((int) (dm.density * 53));
adView.setGravity(Gravity.CENTER);
parentView.addView(adView);

That's it.

The more sophisticated way

In GoodNews though I wanted a more sophisticated way: An "ad loading ..." message should be shown while AdWhirl is busy fetching ads and if it has nothing to fill in an internal ad banner (provided as resource in the app, so that it even works if internet is not available) should be displayed. The loading message is easy, as it can be implemented like shown above, but the dynamic internal banner is a little bit more tricky.

The solution are the mighty custom events provided by AdWhirl which are -- unfortunately -- badly documented. The first step to take is to create a custom event in the AdWhirl web interface:

  1. bring up the "Ad Network Settings"
  2. click the "Ad Custom Event" button at the top
  3. enter an arbitrary name (e.g. "fallback") and function name (the latter one will directly map to a method name in your Java class)
  4. enable the event at give it an allocation of 0%
  5. now bring up the "Backfill Priority" screen and move your fallback event to the end of the list

The configuration above ensures, that your custom event will only be fired when AdWhirl isn't able to show any real ads.

Now you need to handle the event in your code. Therefore you need a class that implements the AdWhirlLayout.AdWhirlInterface and defines a public method without parameters and a name equal to the function name specified by the custom event. This method can then inject your specific view into the AdWhirl layout:

class AdWhirlEventHandler implements AdWhirlLayout.AdWhirlInterface {
    private final AdWhirlLayout adView;

    public AdWhirlEventHandler(AdWhirlLayout adView) {
        this.adView = adView;
    }

    @Override
    public void adWhirlGeneric() {
        // nothing to be done: Generic notifications should also be 
        // configurable in the AdWhirl web interface, but I could't find them
    }    

    /**
     * Will be called by AdWhirl when our custom event with the function name 
     * "fallback" is fired. Called via reflection.
     */
    public void fallback() {
        try {
            final View fallbackView = 
                ... // inflate the view to be shown here
            adView.pushSubView(fallbackView);

            /*
             * reset backfill chain and schedule next ad update
             */
            adView.adWhirlManager.resetRollover();
            adView.rotateThreadedDelayed();
        } catch (MyExpectedException ex) {
            /*
             * forward to next option from the backfill list
             */
            adView.rolloverThreaded();             
        }
    }
}

Now you need to register your event handler with the AdWhirlLayout like this:

adView.setAdWhirlInterface(new AdWhirlEventHandler(adView));

That's it.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!