Android Development Step by step 68:facebook native AD Access Summary

Source: Internet
Author: User

What is the purpose of developing the application? On the one hand is to provide quality service to the user, and on the other hand the most important or need to have profit. Who else has the power to spend time developing apps?

Our application focuses on overseas markets, so it is mainly connected to foreign advertising providers. This article has just finished accessing the Facebook native Ads feature today, introducing how to access FB native ads. For everyone to participate in the test.


First step: Apply for Access account (need FQ)
Https://developers.facebook.com/docs/audience-network/getting-started#company_info
Https://developers.facebook.com/docs/audience-network?

Locale=zh_cn

Step two: Facebook SDK download
Https://developers.facebook.com/docs/android?locale=zh_CN
After downloading, put the Audiencenetwork.jar into the Libs in the project

Step Three: Access documentation
Nativead Single AD
Https://developers.facebook.com/docs/audience-network/android/native-api?locale=zh_CN
Nativeadsmanager Multiple Ads
Https://developers.facebook.com/docs/audience-network/android/nativeadsmanager?locale=zh_CN
Note Because Facebook native ads do not provide an ad address (such as an app or ad's jump address), you need to use this method to trigger an ad jump:
Nativead.registerviewforinteraction (view); This method transmits the ad jump event to its own defined view


Give the 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) {//Load multiple ad final list<nativead> nativeads = new Arraylist<nat        Ivead> (); Final Nativeadsmanager nam = new Nativeadsmanager (fbadvertisementactivity.this, "1542807559327931_1609143346027685",        10); Nam.setlistEner (New Listener () {@Override public void onadsloaded () {int count = Nam.getunique                Nativeadcount ();                    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, "Fbnotificationn            Ativead:onerror: "+ paramaderror.geterrorcode () +", "+ paramaderror.geterrormessage ());        }        });        Nam.loadads ();        Load a single ad 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 ()); Load pictures into memory mainapplication.shared (). Asynimageloader.getimage (Nativead.getadicon (). GETURL (), LRUCache, n EW Onloadimagecallbacklistener () {@Override public void onsuccess (String key, Bitma                    Pdrawable 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 VI            Ewholder = 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 (); }//The data in the data source is populated with the corresponding control mainapplication.shared () in each row. Asynimageloader.loadimageincontainer (nativeads.ge                    T (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 Development Step by step 68:facebook native AD Access Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.