码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-07-24 12:50:41      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:cocos2d-x   eclipse   

   接着上篇继续:

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 Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011      Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
 
http://www.cocos2d-x.org

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, 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 IN
THE 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() {
			@Override
			public void onInterstitialAdReady() {
				Log.i("DomobSDKCocos2d-xDemo", "onAdReady");
				//Toast.makeText(getApplicationContext(),"", Toast.LENGTH_SHORT).show();
			}

			@Override
			public void onLandingPageOpen() {
				Log.i("DomobSDKCocos2d-xDemo", "onLandingPageOpen");
			}

			@Override
			public void onLandingPageClose() {
				Log.i("DomobSDKCocos2d-xDemo", "onLandingPageClose");
			}

			@Override
			public void onInterstitialAdPresent() {
				Log.i("DomobSDKCocos2d-xDemo", "onInterstitialAdPresent");
			}

			@Override
			public void onInterstitialAdDismiss() {
				// Request new ad when the previous interstitial ad
				// was closed.
				mInterstitialAd.loadInterstitialAd();
				Log.i("DomobSDKCocos2d-xDemo", "onInterstitialAdDismiss");
			}

			@Override
			public void onInterstitialAdFailed(ErrorCode errorCode) {
				Log.i("DomobSDKCocos2d-xDemo", "onInterstitialAdFailed");
			}

			@Override
			public void onInterstitialAdLeaveApplication() {
				Log.i("DomobSDKCocos2d-xDemo", "onInterstitialAdLeaveApplication");

			}

			@Override
			public void onInterstitialAdClicked(InterstitialAd interstitialAd) {
				Log.i("DomobSDKCocos2d-xDemo", "onInterstitialAdClicked");
			}
		});
		mInterstitialAd.loadInterstitialAd();
		handler = new Handler() {
			
			@Override
			public void handleMessage(Message msg) {
				
				
				switch (msg.what) {
				/*case 0:// showBanner
					if (bannerLayout.getChildCount() == 0) {
						adView = new AdView(AppActivity.this, PUBLISHER_ID, InlinePPID);
						// set banner size
						adView.setAdSize(AdView.INLINE_SIZE_320X50);
						adView.setKeyword("game");
						adView.setUserGender("male");
						adView.setUserBirthdayStr("2000-08-08");
						adView.setUserPostcode("123456");
						adView.setAdEventListener(new AdEventListener() {
							@Override
							public void onAdOverlayPresented(AdView adView) {
								Log.i("DomobSDKCocos2d-xDemo", "overlayPresented");
							}

							@Override
							public void onAdOverlayDismissed(AdView adView) {
								Log.i("DomobSDKCocos2d-xDemo", "Overrided be dismissed");
							}

							@Override
							public void onAdClicked(AdView adView) {
								Log.i("DomobSDKCocos2d-xDemo", "onDomobAdClicked");
							}

							@Override
							public void onLeaveApplication(AdView adView) {
								Log.i("DomobSDKCocos2d-xDemo", "onDomobLeaveApplication");
							}

							@Override
							public Context onAdRequiresCurrentContext() {
								return null;
							}

							@Override
							public void onAdFailed(AdView adView, ErrorCode errorCode) {
								Log.i("DomobSDKCocos2d-xDemo", "onDomobAdFailed");
							}

							@Override
							public 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");其中要注意你的路径,函数类型,传递参数类型。其他没什么了。我要继续加油,吃午饭了。

还是请大神绕道吧技术分享


版权声明:本文为博主原创文章,未经博主允许不得转载。

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

标签:cocos2d-x   eclipse   

原文地址:http://blog.csdn.net/u011909633/article/details/47038859

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!