Handle AdWhirl onFailure

后端 未结 1 710
再見小時候
再見小時候 2021-01-13 19:34

Hi I am working on an android app and using AdWhirl to display my ads. I want to be able to handle the situation when AdWhirl returns no ads. When it fails, I want to show a

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 20:30

    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:

    
    
        
        
    
    

    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.

    0 讨论(0)
提交回复
热议问题