Cocos2d-x compiled cheer Android app add friend League statistics and Baidu ads code example

Source: Internet
Author: User

A lot of beginners will encounter this problem, often add statistical code or ad code when the error occurs,

This is explained separately from the Android code and the C + + code, and is limited to space, other steps please refer to the relevant configuration documentation.

I searched a lot of code, send a lot of mind debugging, code debugging through, in order to facilitate everyone, directly presented all the source code,

You can use the copy directly.

1. Android code example

In order to be able to display ads using JNI calls to Java code, you need to design a class that provides an interface for invoking advertisements

This class is called: Showadhelper

Package Org.cocos2dx.cpp;import Android.os.handler;import Android.os.message;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;// Send Message display ad Msg.sendtotarget ();} private static void Closemyad () {Message msg = Mhandler.obtainmessage (); msg.what = appactivity.close_ad;// Send Message Close ad Msg.sendtotarget ();}}

At the same time, modify the original activity generated code:

   

/****************************************************************************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 Android.os.bundle;import Android.os.handler;import android.os.Message; Import Android.widget.relativelayout;import Android.widget.relativelayout.layoutparams;import Com.baidu.mobads.adview;import Com.umeng.mobclickcpp.mobclickcpphelper;public class AppActivity extends cocos2dxactivity {public static final int show_ad = 0x0001;public static final int close_ad = 0x0002; AdView AdView; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);  Showadhelper.init (Ahandler); Mobclickcpphelper.init (this);//Friends League statistics}static {mobclickcpphelper.loadlibrary();} private void Closeads () {if (adview!=null) Adview.destroy ();} private void ShowAds () {relativelayout your_originnal_layout = new Relativelayout (this); Addcontentview (your_originnal  _layout, New Layoutparams (Layoutparams.match_parent, layoutparams.match_parent)); Create AD Viewadview = new AdView (this);//Add AdView to the parent control (note: The parent control is not necessarily your root control, as long as the control can add ad views through AddView) Relativelayout.layoutparams RLLP = new Relativelayout.layoutparams (RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT); Rllp.addrule (relativelayout.align_parent_bottom); Your_originnal_ Layout.addview (AdView, RLLP);} Private Handler Ahandler = new Handler () {@Overridepublic void Handlemessage (Message msg) {switch (msg.what) {case Show_ad : ShowAds (); break;case close_ad:closeads (); break;}};}
2.c++ code Example

Next how to call Java in C + + code to display ads, friends of the C + + code will not say, configuration instructions written in detail

The location of the call requires the inclusion of the JNI related header file, the code is as follows:

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

Then write two functions, one for the closure of ads, one to display ads, where needed to call these two functions separately;

The classes here visiblerect themselves to modify as needed.

<pre name= "code" class= "CPP" >void Visiblerect::loadandroidad () {#if (Cc_target_platform = = cc_platform_android) Determine if 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}void Visiblerect::closead () {#if (Cc_target_platform = = cc_platform_android)//Determine whether the ANDROID platform is currently Jnimethodinfo Minfo;  Jobject jobj;  BOOL B = Jnihelper::getstaticmethodinfo (Minfo,   "Org.cocos2dx.cpp/showadhelper", "Closemyad", "() V"); jobj = Minfo.env->callstaticobjectmethod (Minfo.classid, minfo.methodid);  #endif}




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

Cocos2d-x compiled cheer Android app add friend League statistics and Baidu ads code example

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.