嗨,我正在开发一个安卓应用程序,并使用AdWhirl来显示我的广告。我希望能够处理AdWhirl没有返回广告的情况。当它失败时,我想显示一个装饰栏。
有人能给我举个例子吗?
提前谢谢你,
发布于 2011-03-23 17:19:17
好了,我现在已经弄清楚了。有两种可能的方法,一种非常简单,另一种需要更多的工作。
简单的方法
adwhirl布局只要没有什么可显示的就保持不可见。因此,您可以简单地创建一个FrameLayout,其中包含背景中的备用视图和前面的adwhirl视图,如下所示:
<?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>在您的代码中,您可以将视图添加到布局中(其中parentView是上面显示的放大布局):
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);就这样。
更复杂的方式
不过,在GoodNews中,我想要一种更复杂的方式:“广告加载...”当AdWhirl忙于获取广告时,应该显示消息,如果没有什么可以填充的内部广告横幅(作为应用程序中的资源提供,因此即使在互联网不可用的情况下也可以工作)应该显示。加载消息很容易,因为它可以像上面所示的那样实现,但动态内部横幅要稍微复杂一点。
解决方案是AdWhirl提供的强大的自定义事件,不幸的是,这些事件的文档记录得很糟糕。要执行的第一步是在AdWhirl web界面中创建自定义事件:
现在调出顶部的"Ad Network Settings"
it a allocation of 0%
的末尾
上面的配置确保了,您的自定义事件将仅在AdWhirl无法显示任何真实广告时触发。
现在,您需要在代码中处理该事件。因此,您需要一个实现AdWhirlLayout.AdWhirlInterface的类,并定义一个不带参数的公共方法,并定义一个与自定义事件指定的函数名称相同的名称。然后,此方法可以将特定视图注入到AdWhirl布局中:
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();
}
}
}现在,您需要向AdWhirlLayout注册事件处理程序,如下所示:
adView.setAdWhirlInterface(new AdWhirlEventHandler(adView));就这样。
https://stackoverflow.com/questions/5182632
复制相似问题