cocos2dx-3.1 接入多盟广告sdk+Android (3)

来源:互联网 发布:ios10.3.3完美越狱mac 编辑:程序博客网 时间:2024/05/21 10:21

   接着上篇继续:

1、在jni下的android.mk里面添加

LOCAL_SRC_FILES := hellocpp/main.cpp \                   ../../Classes/AppDelegate.cpp \                   ../../Classes/DomobAd.cpp \                   ../../Classes/HelloWorldScene.cpp
2、在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 included inall copies or substantial portions of the Software.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 PARTICULAR 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 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";//你自己应用的ID ,之前说过public static final String InlinePPID = "xxxxxxxx";public static final String InterstitialPPID = "16xxxxxxxx";private static Handler handler;private static RelativeLayout 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);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("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// was closed.mInterstitialAd.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 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");}}
到现在,代码添加完毕,也可以运行代码了,。。。也许途中你会遇见各种问题,比如,程序异常退出,比如调试不进,都要靠自己耐心的去解决。

下面简单介绍一些代码:

在java代码中,你会看见private static Handler handler;代码。handler是java中处理异步消息的机制,之所以接入广告平台会用到handler,是因为,接入广告后,会联网,如果这个操作放入主线程中,界面就会出现假死现象,最后导致android系统“强制关闭”,所以对于这样一个耗时操作,我们把他放在子线程中,但是子线程又设计到UI更新,而对于Android而言,主线程是不安全的,所以handler就是解决这个问题的利器。handler运行在主线程中,它与子线程可以通过消息对象来传递数据。


至于jni,主要调用代码在这: bool ret = JniHelper::getStaticMethodInfo(info,"org/cocos2dx/cpp/TestJni","func1","()V");其中要注意你的路径,函数类型,传递参数类型。其他没什么了。我要继续加油,吃午饭了。

尴尬


1 0
原创粉丝点击