android开发步步为营之68:Facebook原生广告接入总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了android开发步步为营之68:Facebook原生广告接入总结相关的知识,希望对你有一定的参考价值。
开发应用的目的是干嘛?一方面当然是提供优质服务给用户,还有一方面最重要的还是须要有盈利。不然谁还有动力花钱花时间去开发app?
我们的应用主攻海外市场,所以主要还是接入国外的广告提供商。本文就今天刚完毕接入facebook原生广告功能,介绍一下怎样接入fb的原生广告。供大家參考。
第一步:申请接入账号(须要FQ)
https://developers.facebook.com/docs/audience-network/getting-started#company_info
https://developers.facebook.com/docs/audience-network?
locale=zh_CN
第二步:facebook sdk下载
https://developers.facebook.com/docs/android?locale=zh_CN
下载完毕后将AudienceNetwork.jar放入项目中的libs中
第三步:接入參考文档
NativeAd单条广告
https://developers.facebook.com/docs/audience-network/android/native-api?locale=zh_CN
NativeAdsManager多条广告
https://developers.facebook.com/docs/audience-network/android/nativeadsmanager?locale=zh_CN
注意由于facebook原生广告没有提供广告地址(比方app下载地址或者广告的跳转地址),须要使用这种方法来触发广告的跳转:
nativeAd.registerViewForInteraction(view); 该方法将广告跳转事件传给自己定义的view
给出demo:
/** * */ package com.figo.study; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import com.figo.study.bitmapcache.core.AsynImageLoader.LoadingListener; import com.figo.study.bitmapcache.core.AsynImageLoader.OnLoadImageCallBackListener; import com.facebook.ads.Ad; import com.facebook.ads.AdError; import com.facebook.ads.AdListener; import com.facebook.ads.NativeAd; import com.facebook.ads.NativeAdsManager; import com.facebook.ads.NativeAdsManager.Listener; import com.figo.study.utils.CommonUtils; /** * @author figo * */ public class FbAdvertisementActivity extends Activity { private String tag = "FbAdvertisementActivity"; private ImageView imgLogo; private TextView tvTitle, tvContent; private LinearLayout layout_ad; private ListView lvFbAds; List<NativeAd> nativeAds; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_facebookad); layout_ad = (LinearLayout) findViewById(R.id.layout_ad); imgLogo = (ImageView) findViewById(R.id.img_logo); tvTitle = (TextView) findViewById(R.id.tv_title); tvContent = (TextView) findViewById(R.id.tv_content); lvFbAds = (ListView) findViewById(R.id.lv_fbads); loadAd("1542807559327931_1609143346027685"); } private void loadAd(String adId) { //载入多条广告 final List<NativeAd> nativeAds = new ArrayList<NativeAd>(); final NativeAdsManager nam = new NativeAdsManager(FbAdvertisementActivity.this, "1542807559327931_1609143346027685", 10); nam.setListener(new Listener() { @Override public void onAdsLoaded() { int count = nam.getUniqueNativeAdCount(); while (count > 0) { nativeAds.add(nam.nextNativeAd()); count--; } FbAdAdapter fba = new FbAdAdapter(nativeAds, FbAdvertisementActivity.this); lvFbAds.setAdapter(fba); } @Override public void onAdError(AdError paramAdError) { Log.e(tag, "FbNotificationNativeAd:onError:" + paramAdError.getErrorCode() + ", " + paramAdError.getErrorMessage()); } }); nam.loadAds(); //载入单条广告 final NativeAd nativeAd = new NativeAd(FbAdvertisementActivity.this, adId); nativeAd.setAdListener(new AdListener() { @Override public void onError(Ad ad, AdError error) { Log.e(tag, "FbNotificationNativeAd:onError:" + error.getErrorCode() + ", " + error.getErrorMessage()); } @Override public void onAdLoaded(final Ad ad) { if (ad != nativeAd) return; if (nativeAd.getAdIcon() == null || nativeAd.getAdIcon().getUrl() == null) return; tvTitle.setText(nativeAd.getAdTitle()); tvContent.setText(nativeAd.getAdSubtitle()); //通过lrucache载入图片到内存 MainApplication.shared().asynImageLoader.getImage(nativeAd.getAdIcon().getUrl(), 200, new OnLoadImageCallBackListener() { @Override public void onSuccess(String key, BitmapDrawable bitmap) { imgLogo.setImageDrawable(bitmap); } @Override public void onFailure(String key, int errorType) { } }); nativeAd.registerViewForInteraction(layout_ad); } @Override public void onAdClicked(Ad ad) { } }); nativeAd.loadAd(); } class FbAdAdapter extends BaseAdapter { List<NativeAd> nativeAds; Context mContext; public FbAdAdapter(List<NativeAd> fbNativeAds, Context context) { nativeAds = fbNativeAds; mContext = context; } @Override public int getCount() { return nativeAds.size(); } @Override public Object getItem(int position) { return nativeAds.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.listitem_facebookad, parent, false); viewHolder = new ViewHolder(); viewHolder.imgLogo = (ImageView) convertView.findViewById(R.id.img_logo); viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title); viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_content); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } // 将数据源中的数据填充到每一行中相应的控件 MainApplication.shared().asynImageLoader.loadImageInContainer(nativeAds.get(position).getAdIcon().getUrl(), viewHolder.imgLogo, CommonUtils.dip2px(FbAdvertisementActivity.this, 72), getResources().getDrawable(R.drawable.ic_launcher), getResources().getDrawable(R.drawable.ic_launcher), new LoadingListener() { @Override public void onLoadStart() { } @Override public void onLoadInProgress(int progress) { } @Override public void onLoadEnd(boolean isSuccess) { notifyDataSetChanged(); } }); viewHolder.tvTitle.setText(nativeAds.get(position).getAdTitle()); viewHolder.tvContent.setText(nativeAds.get(position).getAdSubtitle()); nativeAds.get(position).registerViewForInteraction(convertView); return convertView; } } class ViewHolder { ImageView imgLogo; TextView tvTitle; TextView tvContent; } }
以上是关于android开发步步为营之68:Facebook原生广告接入总结的主要内容,如果未能解决你的问题,请参考以下文章
android framework开发之广播broadcast源码分析2-千里马