Cocos2d-x Study Notes (v) Access Baidu affiliate ADS (Android banner and interstitial ads)

Source: Internet
Author: User

With Cocos2d-x 3.4 developed a small game, and then successfully access the Baidu Alliance ads, many beginners also want to develop the game in their own access to advertising profits, so I will Baidu Alliance banner and interstitial ads for example, according to their own access to the steps to do a summary, I hope that the need for developers to help.

First, Baidu SDK package Import and configuration of permissions

1. Import Jar Package

Download Baidu Alliance Android SDK Package, I downloaded the filename baidu_union_sdk_android.zip, unzip the folder inside there is a jar package Baidu_mobads_ Sdk.jar, copy the jar package to your project's libs directory (if you don't have a new Libs folder), then right-click on your project->properties->java Build path->libraries-> Add jars adds the Baidu jar package as shown in:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/95/wKiom1YBTTbhWC-3AAHXkG1q-Yo732.jpg "title=" QQ Pinyin is not named. png "alt=" wkiom1ybttbhwc-3aahxkg1q-yo732.jpg "/>

2, Configuration Androidmanifest.xml

Add permissions

<uses-permission android:name= "Android.permission.INTERNET"  /><uses-permission android : Name= "Android.permission.READ_PHONE_STATE"  /><uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE " /><uses-permission android:name=" Android.permission.ACCESS_COARSE_LOCATION " /><uses-permission android:name=" Android.permission.WRITE_EXTERNAL_STORAGE " /><uses-permission android:name=" Android.permission.ACCESS_WIFI_STATE " /><!--  The following are optional permissions  --><uses-permission  Android:name= "Android.permission.CHANGE_WIFI_STATE"  /><uses-permission android:name= " Android.permission.RECORD_AUDIO " /><uses-permission android:name=" Android.permission.VIBRATE "  /><uses-permission android:name= "Android.permission.CAMERA"  /><uses-permission  android:name= "Android.permission.ACCESS_FINE_LOCATION"  /><uses-permission androiD:name= "Com.android.browser.permission.READ_HISTORY_BOOKMARKS"  /> 

Add Appsid and Appactivity declarations

<meta-data android:name= "baidumobad_app_id" android:value= "DCD69C04"/><meta-data android:name= " Baidumobad_app_sec "android:value=" DCD69C04 "/><activity android:name=" Com.baidu.mobads.AppActivity "Android : configchanges= "Keyboard|keyboardhidden|orientation"/>

As shown in the following:

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/92/wKioL1YBT1aBrdfRAAJ9S5RSVew859.jpg "title=" QQ Pinyin is not named. png "alt=" wkiol1ybt1abrdfraaj9s5rsvew859.jpg "/>


Second, the banner banner advertising access

Add the following code to the Appactivity.java file

Private AdView AdView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setupads ();} private void Setupads () {relativelayout layout = new Relativelayout (this); Addcontentview (layout, New Layoutparams ( layoutparams.match_parent,layoutparams.match_parent)); AdView = new AdView (this); Relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (layoutparams.match_parent, layoutparams.wrap_content); Layoutparams.addrule (Relativelayout.align_parent_bottom); Layout.addView (AdView, Layoutparams);}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/95/wKiom1YBUt6xjJF7AAOaxjQDuDk796.jpg "title=" QQ Pinyin is not named. png "alt=" wkiom1ybut6xjjf7aaoaxjqdudk796.jpg "/> The ad will appear at the bottom of the game after adding code


Third, join interstitial advertisement

1, in the class appactivity the same package name new Showadhelper class file

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/92/wKioL1YBVLHygqp5AABKlRL_2Cs059.jpg "title=" QQ Pinyin is not named. png "alt=" wkiol1ybvlhygqp5aabklrl_2cs059.jpg "/>

2. Add the following code to the class Showadhelper

public class Showadhelper {private static Handler Mhandler;      public static void Init (Handler Handler) {showadhelper.mhandler = Handler;          } private static void Showmyad () {Message msg = Mhandler.obtainmessage ();          Msg.what = Appactivity.show_ad;      Msg.sendtotarget (); }  }

3. Define a variable and a constant in class Showadhelper

public static final int show_ad = 0x0001;interstitialad Interad;

3. New Getinsertad () method in class Appactivity initialize interstitial ad

Public Interstitialad Getinsertad () {final Interstitialad interAd1 = new Interstitialad (this); Interad1.setlistener (new Interstitialadlistener () {@Overridepublic void Onadclick (Interstitialad arg0) {log.i ("Interstitialad", "Onadclick");} @Overridepublic void onaddismissed () {log.i ("Interstitialad", "onaddismissed"); Interad1.loadad ();} @Overridepublic void onadfailed (String arg0) {log.i ("Interstitialad", "onadfailed");} @Overridepublic void Onadpresent () {log.i ("Interstitialad", "onadpresent");} @Overridepublic void Onadready () {log.i ("Interstitialad", "Onadready");}); Interad1.loadad (); return interAd1;}

4. Create Handler

Private Handler Ahandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case Show_ad : if (Interad.isadready ()) {Interad.showad (appactivity.this); LOG.D ("LC", "Show");} else {Interad.loadad ();} LOG.D ("LC", "not show");

5. Add code to OnCreate

@Overrideprotected void OnCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate ( savedinstancestate); Interad = Getinsertad (); Showadhelper.init (Ahandler);}

6. The Java layer is written, and then the Showmyad function in the Showadhelper class is called through JNI in the C + + layer

void Backgroundlayer::loadandroidad () {#if (Cc_target_platform = = cc_platform_android)// Determine whether the current Android platform is jnimethodinfo minfo;jobject jobj;bool b = jnihelper::getstaticmethodinfo (Minfo, "org/cocos2dx/cpp/ Showadhelper "," Showmyad "," () V "), Jobj = Minfo.env->callstaticobjectmethod (Minfo.classid, minfo.methodid); #endif }

Also add at the beginning of the Backgroundlayer file

#if (Cc_target_platform = = cc_platform_android) #include <jni.h> #include "platform/android/jni/jnihelper.h" # endif

Then call the Loadandroidad () function where you want the interstitial ad to appear

Cocos2d-x Study Notes (v) Access Baidu affiliate ADS (Android banner and interstitial ads)

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.