Cocos2dx Android integrates mango interstitial ad and cocos2dxandroid
After crawling through various pitfalls, we finally had a decent little game! Then, the next job is to integrate advertising and make profits online.
Mango is an integrated advertising platform that allows you to access mango and then display many of the frequently used advertisements, such as domeng, Baidu, and AdMob. This is very convenient.
1. Ad platform configuration 1.1 registered mango account: http://www.adsmogo.com/
Go to mango and choose App Management> Add App. fill in all the information and click "add App" at the bottom. The process is simple and I will not.
Click the App list to view the App we just added. Click the App name to enter the App configuration. Here, we will suspend it. Since mango is an aggregated advertising platform that displays advertisements from other platforms, we need to apply for an App on other platforms, here we choose domob. This platform is free. You can choose multiple platforms based on your needs.
1.2 registered domob: http://www.domob.cn/
Go to the domob developer background, click my application> Add application, and enter the application information.
Add ad spaces. Note that one application can have multiple ad spaces ~ For ad space types, select ordinary ad space on the interstitial screen.
Then we can get the publisher ID and ad space ID. The configuration of the domob platform is complete.
1.3 enter publisherID and ad space ID
Return to mango, go to the App configuration page, select full screen/interstitial screen, click domu, and enter publisher ID and ad space ID.
After saving the settings, check the priority and complete the configuration on the mango platform ~ Write down the app ID of mango, which we need to use when accessing advertisements.
2. Import the SDK
First, create a cocos2dx project to ensure that the project can be compiled to Android. You don't need to introduce it to me (* ^__ ^ *)
Download the sdk of Mango cocos2dx and the sdks of domob and find the jar package we need.
Open eclipse and import the project. Here I am AndroidTest. Right-click the libs directory (if there is no libs, create one), select import-> General-> File System, and click next
Find and import the sdks of mango and domob.
Right-click the two jar files in the libs folder and choose Build Path> Add to Build Path.
3. Add User permission 3.1 open AndroidManifest. xml and add the following code after the <application> tag:
<! -- Add User Permissions --> <! -- Connect to the INTERNET with the network permission and use the allow command to request an advertisement (required). mogo will use this permission --> <uses-permission android: name = "android. permission. INTERNET"/> <! -- Read the basic information permission of the mobile phone READ_PHONE_STATE, use the token to accurately collect information such as the user's model (required), mogo will use this permission --> <uses-permission android: name = "android. permission. READ_PHONE_STATE "/> <! -- ACCESS_NETWORK_STATE is the permission to read network information. It is required to use the token to identify the Access Point of the gprs network. mogo uses this permission --> <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE "/> <! -- Obtain the geographical information permissions of users, and use the statement to help the inferior to accurately count the distribution of users after the developer's sequential release of the statement, mogo will use this permission --> <uses-permission android: name = "android. permission. ACCESS_FINE_LOCATION "/> <uses-permission android: name =" android. permission. ACCESS_COARSE_LOCATION "/> <uses-permission android: name =" android. permission. ACCESS_LOCATION_EXTRA_COMMANDS "/> <! -- Read and write permissions on the sd card, use the token to cache the images used by the advertisement, save traffic, and get a better user experience, mogo will use this permission --> <uses-permission android: name = "android. permission. WRITE_EXTERNAL_STORAGE "/> <uses-permission android: name =" android. permission. READ_EXTERNAL_STORAGE "/> <! -- Obtain the current wifi status. This permission is required for the advertising platform. mogo uses this permission --> <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE "/> <! -- The permission required for the pay-as-you-go function. mogo uses this permission --> <uses-permission android: name = "android. permission. GET_TASKS"/> <! -- Do not have the required permissions. do not have this permission. Do not display the announcement --> <uses-permission android: name = "android. permission. VIBRATE"/>
As shown in:
3.2 declare the Mogo Activity and insert the following code in the <application> tag:
<! -- Declare Mogo Activity --> <activityandroid: name = "com. adsmogo. adview. adsMogoWebView "android: configChanges =" orientation | keyboardHidden | screenSize "android: label =" @ string/app_name "/> <activity android: name =" com. adsmogo. mriad. view. adsMogoActionHandler "android: configChanges =" orientation | keyboardHidden | screenSize "android: label =" @ string/app_name "> </activity> <! -- Register the Service, which is required for the application download function of independent advertisements without exchanging Advertisements --> <service android: name = "com. adsmogo. controller. service. updateService "android: exported =" true "android: process =": remote "/> <! -- Register the Service and install the application --> <service android: name = "com. adsmogo. controller. service. countService "android: exported =" true "android: process =": remote "/>
As shown in:
4. Advertisement placement
4.1 Add the following code to the onCreate function of Cocos2dxActivity:
<Span style = "white-space: pre"> </span> AdsMogoInterstitialManager. setDefaultInitManualRefresh (false); // set AppKeyAdsMogoInterstitialManager. setDefaultInitAppKey ("mogoID"); // sets the current Activity object AdsMogoInterstitialManager. setInitActivity (Cocos2dxActivity. this); // initialization (you must first set the default AppKey and Activity object to use this method to initialize the SDK) AdsMogoInterstitialManager. your instance (). initdefainterinterstitial (); handler = new Handler () {@ Overri Depublic void handleMessage (Message msg) {// TODO Auto-generated method stubswitch (msg. what) {case 3:/*** enter display time * Call interstitialShow (boolean isWait) when the application needs to display full-screen ads; * notify the SDK to enter the display time, the SDK will do its utmost to display the advertisement. Of course, the * advertisement cannot be displayed immediately due to network problems. You can use the isWait parameter to control the SDK to display the advertisement immediately after obtaining the advertisement. */AdsMogoInterstitialManager. your instance (). defaultInterstitial (). interstitialShow (true); break; case 5:/*** exit display time * If you have entered the display time before and set the isWait parameter to YES, call interstitialCancel () to notify SDK */AdsMogoInterstitialManager. your instance (). defaultInterstitial (). interstitialCancel (); break; default: break ;}}};
Replace the mogoID with the Application ID applied on the mango platform.
4.2 Add the following functions to Cocos2dxActivity:
<Span style = "white-space: pre"> </span> // exit display time public static void closeInterstitial () {Message msg = handler. obtainMessage (); msg. what = 5; handler. sendMessage (msg);} // send the public static void showInterstitialStatic () {Log. e ("MogoCocos2dx Demo", "showInterstitialStatic"); Message msg = handler. obtainMessage (); msg. what = 3; handler. sendMessage (msg) ;}@ Overrideprotected void onDestroy () {super. OnDestroy (); AdsMogoLayout. clear (); if (adView! = Null) {adView. clearthur read ();} Log. e ("AdsMogo", "onDestroy"); System. exit (0 );}
4.3 create a C ++ File
Create MOGOAd. h and MOGOAd. cpp in the classes directory.
The content of MOGOAd. h is:
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)#include <jni.h>#include "platform/android/jni/JniHelper.h"#include <android/log.h>#endif#include "cocos2d.h" using namespace cocos2d;class MOGOAd{public:static void showInterstitial();static void closeInterstitial(); };
The content of MOGOAd. cpp is:
#include "MOGOAd.h"void MOGOAd::showInterstitial(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo showInterstitial;bool isHave = JniHelper::getStaticMethodInfo(showInterstitial,"org/cocos2dx/cpp/Cocos2dxActivity","showInterstitialStatic","()V"); if (!isHave) { CCLog("jni:hideBannerStatic false");}else{CCLog("jni:hideBannerStatic true");showInterstitial.env->CallStaticVoidMethod(showInterstitial.classID, showInterstitial.methodID); } #endif}void MOGOAd::closeInterstitial(){ #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) JniMethodInfo closeInterstitial;bool isHave = JniHelper::getStaticMethodInfo(closeInterstitial,"org/cocos2dx/cpp/Cocos2dxActivity","closeInterstitial","()V"); if (!isHave) { CCLog("jni:hideBannerStatic false");}else{CCLog("jni:hideBannerStatic true");closeInterstitial.env->CallStaticVoidMethod(closeInterstitial.classID, closeInterstitial.methodID); } #endif}
4.4 change the button method in HelloWorldScene. cpp:
void HelloWorld::menuCloseCallback(Ref* pSender){ MOGOAd::showInterstitial();}
OK, merit is complete. Add MOGOAd. cpp to Android. mk to compile and run it.
Successful! Next, submit the application for review. Wait until the application passes ~