COCOS2D-X编译成安卓APP添加友盟统计和百度广告的代码示例

前端之家收集整理的这篇文章主要介绍了COCOS2D-X编译成安卓APP添加友盟统计和百度广告的代码示例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

对初学者很多都会遇到这个问题,经常加统计代码或广告代码的时候会出错,

这里从安卓代码和C++代码分别说明,限于篇幅,其他步骤请参考相关的配置说明文档。

自己搜了很多代码,发很多心思调试整理,代码都调试通过,为了方便大家,直接献上所有源代码

大家直接COPY用就可以了。

1.安卓代码示例

为了能够用JNI调用JAVA代码显示广告,需要设计一个类,提供调用广告的接口

这个类叫: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;//发送消息显示广告
		msg.sendToTarget();
	}
	private static void closeMyAd() {
		Message msg = mHandler.obtainMessage();
		msg.what = AppActivity.CLOSE_AD;//发送消息关闭广告
		msg.sendToTarget();
	}
}

同时,再修改原来Activity生成代码

/****************************************************************************
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 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;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ShowAdHelper.init(aHandler);  
		MobClickCppHelper.init(this);//友盟统计
	}

	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));  
		// 创建广告View
		adView = new AdView(this);
		

		// 将adView添加到父控件中(注:该父控件不一定为您的根控件,只要该控件能通过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() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case SHOW_AD:
				showAds();
				break;
			case CLOSE_AD:
				closeAds();
				break;				
			}
		}
	};
}
2.C++代码示例

接下来讲怎么用C++代码调用JAVA显示广告,友盟的C++代码就不说了,配置说明写的很详细

调用的地方需进行JNI相关头文件的包含,代码如下:

#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#endif

然后再写两个函数,一个用于关闭广告,一个显示广告,在需要的地方分别调用这两个函数就可以了;

这里的类VisibleRect自己根据需要修改

<pre name="code" class="cpp">void VisibleRect::loadAndroidAd()
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台 
	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) //判断当前是否为Android平台 
	JniMethodInfo minfo;  
	jobject jobj;  
	bool b = JniHelper::getStaticMethodInfo(minfo,"closeMyAd",minfo.methodID);  
#endif 
}
原文链接:https://www.f2er.com/cocos2dx/341591.html

猜你在找的Cocos2d-x相关文章