Cocos2dx-3.1 access domob ad sdk + Android (3), cocos2dxandroidsdk
Continue with the previous article:
1. Add it to android. mk under jni
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) 2011 Zynga Inc. copyright (c) 2013-2014 Chukong Technologies Inc. http://www.cocos2d-x.orgPermission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software "), To dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be encoded inall copies or substantial portions of the So Ftware. the software is provided "as is", without warranty of any kind, express orimplied, including but not limited to the warranties of merchantability, fitness for a particle purpose and noninfringement. in no event shall theauthors or copyright holders be liable for any claim, damages or otherliability, whether in an action of contract, tort or otherwise, arising from, OUT OF OR IN CONNECTION W Ith 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 Cocos2d XActivity {public static final String PUBLISHER_ID = "xxxxxxx"; // the ID of your own application, which was previously mentioned as public static final String InlinePPID = "xxxxxxxx "; public static final String Signature = "16 xxxxxxxx"; private static Handler handler; private static RelativeLayout bannerLayout; private AdView adView; private InterstitialAd metadata; protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInst AnceState); bannerLayout = new RelativeLayout (this); RelativeLayout. layoutParams parentLayputParams = new RelativeLayout. layoutParams (RelativeLayout. layoutParams. MATCH_PARENT, RelativeLayout. layoutParams. MATCH_PARENT); this. addContentView (bannerLayout, parentLayputParams); if (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 ("DomobSDKCocos 2d-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 // was closed. mInterstitialAd. loadInterstitialAd (); Log. I ("DomobS DKCocos2d-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 adVie W) {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 (AdV Iew 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. addVi Ew (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 not 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, 16, 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 ");}}
Now, after the code is added, you can run the code ,... You may encounter various problems on the way, such as program exit exceptions, such as debugging failure, which must be solved with patience.
The following code is a brief introduction:
In java code, you will see private static Handler handler; code. Handler is a mechanism for processing asynchronous messages in java. handler is used when an ad platform is connected because it is connected to the Internet. If this operation is placed in the main thread, the interface will be suspended, causing the android system to "force close". Therefore, for such a time-consuming operation, we put it in the Child thread, but the child thread is designed for UI update, for Android, the main thread is insecure, so handler is a tool to solve this problem. Handler runs in the main thread. It and the subthread can transmit data through message objects.
As for jni, the main calling code is here:
bool
ret = JniHelper::getStaticMethodInfo(info,
"org/cocos2dx/cpp/TestJni"
,
"func1"
,
"()V"
). Pay attention to your path, function type, and parameter type. Nothing else. I want to continue to cheer for lunch.
Please bypass me
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.