cocos2dx-3.1 access to multi-League advertising Sdk+android (3)

Source: Internet
Author: User

Then the previous article continued:

1, in the android.mk under the JNI add

Local_src_files: = Hellocpp/main.cpp                    . /.. /classes/appdelegate.cpp.                    /.. /classes/domobad.cpp.                    /.. /classes/helloworldscene.cpp
2. Add the following code under Appactivity:

/****************************************************************************copyright (c) 2008-2010 Ricardo  Quesadacopyright (c) 2010-2012 Cocos2d-x.orgcopyright (c) Zynga Inc.copyright (c) 2013-2014 Chukong Technologies  Inc. Http://www.cocos2d-x.orgPermission is hereby granted, free of charge, to all person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the software without restriction, including without Limita tion the Rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the software, and to per MIT persons to whom the software isfurnished to doing so, subject to the following conditions:the above copyright notice and  This permission notice shall being included inall copies or substantial portions of the Software.the software is provided "as Is ', without WARRANTY of any KIND, EXPRESS orimplied, including and not LIMITED to the warranties of MERCHANTABILITY,FITN ESS for A particular PURPOSE and NONINfringement.  In NO EVENT shall theauthors or COPYRIGHT holders is liable for any CLAIM, damages OR otherliability, WHETHER in an ACTION  of contract, TORT or OTHERWISE, arising from,out of or in CONNECTION with the software or the use or other dealings inthe Software.****************************************************************************/package Org.cocos2dx.cpp; Import Org.cocos2dx.lib.cocos2dxactivity;import Org.cocos2dx.lib.cocos2dxglsurfaceview;import Cn.domob.android.ads.adeventlistener;import Cn.domob.android.ads.adview;import Cn.domob.android.ads.interstitialad;import Cn.domob.android.ads.interstitialadlistener;import Cn.domob.android.ads.admanager.errorcode;import Android.annotation.suppresslint;import Android.content.Context; Import Android.os.bundle;import android.os.handler;import Android.os.message;import Android.util.log;import Android.view.view;import android.widget.relativelayout;import android.widget.Toast; @SuppressLint ("Handlerleak") public class Appactivity extends Cocos2dxactivity {public static final string publisher_id = "xxxxxxx";//the ID of your own application, previously said public static final string inlineppid = "xxxxxxxx";p ublic static final String interstitialppid = "16xxxxxxxx";p rivate static Handler handler;private static Rel Ativelayout bannerlayout;private AdView adview;private interstitialad minterstitialad;protected void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); bannerlayout = new Relativelayout (this); Relativelayout.layoutparams parentlayputparams = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.MATCH _parent, RelativeLayout.LayoutParams.MATCH_PARENT); This.addcontentview (Bannerlayout, parentlayputparams); Minterstitialad = null) {return;} Minterstitialad = new Interstitialad (Appactivity.this, publisher_id, interstitialppid); Minterstitialad.setinterstitialadlistener (New Interstitialadlistener () {@Overridepublic void Oninterstitialadready () {log.i ("Domobsdkcocos2d-xdemo", "Onadready");//toast.maketext (GetapplicationcontexT (), "", Toast.length_short). Show (); @Overridepublic void Onlandingpageopen () {log.i ("Domobsdkcocos2d-xdemo", "Onlandingpageopen");} @Overridepublic void Onlandingpageclose () {log.i ("Domobsdkcocos2d-xdemo", "Onlandingpageclose");} @Overridepublic void Oninterstitialadpresent () {log.i ("Domobsdkcocos2d-xdemo", "oninterstitialadpresent");} @Overridepublic void Oninterstitialaddismiss () {//Request new AD when the previous interstitial ad//is Closed.mintersti Tialad.loadinterstitialad (); LOG.I ("Domobsdkcocos2d-xdemo", "Oninterstitialaddismiss");} @Overridepublic void oninterstitialadfailed (ErrorCode ErrorCode) {log.i ("Domobsdkcocos2d-xdemo", " Oninterstitialadfailed ");} @Overridepublic void Oninterstitialadleaveapplication () {log.i ("Domobsdkcocos2d-xdemo", " Oninterstitialadleaveapplication ");} @Overridepublic void oninterstitialadclicked (Interstitialad interstitialad) {log.i ("Domobsdkcocos2d-xdemo", " Oninterstitialadclicked ");}); Minterstitialad.loadinterstitialad (); handler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {/*case 0://Showbannerif (bannerlayout.getchildcount () = = 0 {AdView = new AdView (Appactivity.this, publisher_id, inlineppid);//Set Banner Sizeadview.setadsize (Adview.inline_ size_320x50), Adview.setkeyword ("game"), Adview.setusergender ("male"); Adview.setuserbirthdaystr ("2000-08-08"); Adview.setuserpostcode ("123456"); Adview.setadeventlistener (new Adeventlistener () {@Overridepublic void onadoverlaypresented (AdView AdView) {log.i ("Domobsdkcocos2d-xdemo", "overlaypresented");} @Overridepublic void onadoverlaydismissed (AdView AdView) {log.i ("Domobsdkcocos2d-xdemo", "overrided be Dismissed");} @Overridepublic void onadclicked (AdView AdView) {log.i ("Domobsdkcocos2d-xdemo", "ondomobadclicked");} @Overridepublic void Onleaveapplication (AdView AdView) {log.i ("Domobsdkcocos2d-xdemo", "ondomobleaveapplication");} @Overridepublic Context Onadrequirescurrentcontext () {return null;} @Overridepublic void onadfailed (AdView AdView, ErrorCode Errorcode) {log.i ("Domobsdkcocos2d-xdemo", "ondomobadfailed");} @Overridepublic void oneventadreturned (AdView AdView) {log.i ("Domobsdkcocos2d-xdemo", "ondomobadreturned");}); Relativelayout.layoutparams layoutparams = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.WRAP_ CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); Layoutparams.addrule (Relativelayout.align_parent_bottom, Relativelayout.true); Layoutparams.addrule (Relativelayout.center_horizontal, relativelayout.true); Bannerlayout.addview (AdView, layoutparams);} else {if (AdView! = null) {adview.setvisibility (view.visible); Adview.requestrefreshad ();}} Break;case 1:if (AdView! = null) {adview.setvisibility (view.gone);} Break;*///case 2://break;case 3:if (Minterstitialad! = null) {if (Minterstitialad.isinterstitialadready ()) { Minterstitialad.showinterstitialad (appactivity.this);} else {log.i ("Domobsdkcocos2d-xdemo", "interstitial Ad is isn't ready"); Minterstitialad.loadinterstitialad ();}} else {log.i ("Domobsdkcocos2d-xdemo", "InterstiTial Ad is not init ");} Break;default:break;}}};} Public Cocos2dxglsurfaceview Oncreateview () {Cocos2dxglsurfaceview Glsurfaceview = new Cocos2dxglsurfaceview (this); Glsurfaceview.seteglconfigchooser (5, 6, 5, 0, +, 8); return Glsurfaceview;} public static void Showbannerstatic () {//handler.sendemptymessage (0);//}//public static void Hidebannerstatic () {// Handler.sendemptymessage (1);//}//public static void Initinterstitialstatic () {//handler.sendemptymessage (2);/} public static void Showinterstitialstatic () {handler.sendemptymessage (3);} static {system.loadlibrary ("Cocos2dcpp");}}
By now, the code has been added, you can run the code, ... Perhaps you will encounter a variety of problems on the way, for example, the program abnormal exit, such as debugging does not enter, all rely on their patience to solve.

Here's a quick introduction to some code:

In Java code, you will see the private static Handler Handler; Handler is the mechanism of processing asynchronous messages in Java, the reason for access to the advertising platform will be used handler, because, access to advertising, will be networked, if this operation into the main thread, the interface will appear suspended animation phenomenon, and finally led to the Android system "forced shutdown", So for a time-consuming operation, we put him in a sub-thread, but the child thread is designed to update the UI, and for Android, the main thread is unsafe, so handler is the tool to solve the problem. Handler runs in the main thread, and it can pass data through the message object to the child thread.


As for JNI, the main calling code is here:  bool ret = Jnihelper::getstaticmethodinfo (info, " Org/cocos2dx/cpp/testjni ", " func1 ", " () V ");其中要注意你的路径,函数类型,传递参数类型。其他没什么了。我要继续加油,吃午饭了。

Let the great God take a detour.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

cocos2dx-3.1 access to multi-League ads Sdk+android (3)

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.