我正在使用适用于 Android 的 Facebook Audience Network 4.14.1,并尝试在用户点击来自 Facebook 的广告后在我的应用程序上执行逻辑。我使用的是 4.11.x 版本,AdListener 运行良好。出于某种原因,在某些版本之后它不再工作了。有没有不同的方法来使用它? 加载广告后,我只需像这样注册一个监听器:
nativeAd.setAdListener(new AdListener() {
@Override
public void onError(Ad ad, AdError adError) {
}
@Override
public void onAdLoaded(Ad ad) {
}
@Override
public void onAdClicked(Ad ad) {
// my logic
}
});
最佳答案
有同样的问题,这似乎是 Facebook SDK 中的一个公认错误: https://developers.facebook.com/bugs/158853171214759/
Facebook 员工的最新回复是:
After investigating this with the engineering teams, we've decided not to release a fix for this specific issue at this time. However, we have some planned changes that should mitigate this issue in future versions of the SDK.
谢谢,Facebook。
更新
我能够通过创建一个自定义的 FrameLayout 来检测它的点击来解决它,它是 hacky,并不完美,但总比什么都没有好。
public class AdContainer extends FrameLayout implements OnGestureListener {
GestureDetector clickDetector;
private NativeAd ad;
private AdListener listener;
public AdContainer(@NonNull Context context) {
super(context);
init();
}
public AdContainer(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public AdContainer(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@TargetApi(VERSION_CODES.LOLLIPOP)
public AdContainer(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr, @StyleRes int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
clickDetector = new GestureDetector(getContext(), this);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
clickDetector.onTouchEvent(ev);
return super.onInterceptTouchEvent(ev);
}
public void setAd(NativeAd ad, AdListener listener) {
this.ad = ad;
this.listener = listener;
}
// OnGestureListener
@Override
public boolean onSingleTapUp(MotionEvent e) {
Log.d("AdContainer", "detected a click in an ad container: " + ad);
if ((ad != null) && (listener != null)) {
listener.onAdClicked(ad);
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
return false;
}
@Override
public void onShowPress(MotionEvent e) {
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
return false;
}
@Override
public void onLongPress(MotionEvent e) {
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
}
像这样使用它:
(1) 将您的广告布局膨胀到新的容器类中:
<com.example.AdContainer
android:id="@+id/ad_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
(2) 将您的广告绑定(bind)到布局时,将其注册到 AdContainer
:
AdContainer container = (ViewGroup) findViewById(R.id.ad_container);
container.setAd(ad, this); // make sure the current class implements AdListener
关于android - Facebook Audience Network - 带有 AdListener 的 NativeAdsManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38881749/