关于在cocos2d-x中插入第三方广告
1、在点金平台
appSec 应用在通过审核之后给出的一个唯一标识码
cocos2d通过jnihelper类调用java接口方法
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
#include <jni.h>
#include "platform/android/jni/JniHelper.h"
#endif
//判断当前是否为Android平台
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//定义Jni函数信息结构体
JniMethodInfo minfo;
//返回一个bool值表示是否找到此函数
bool isHave = JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/application/ApplicationDemo","testFun", "()V");
if (isHave) {
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
#endif
//上面的方法已经通过测试没有问题,具体可以看下面的blog或参考API文档
http://www.himigame.com/android-game/725.html
2、我在cocos2d-x的项目中自己专门做了一个调用第三方广告的类:
头文件是:CMAdvertisement.h
#ifndef _CMADVERTISEMENT_H_
#define _CMADVERTISEMENT_H_
//该类是支持第三方广告平台的类,独立与游戏本身的
#include "GameCommon.h"
class CMAdvertisement{
public:
static void getDianJinAdvertisement();//获得点金广告平台
static void destroyDianJinAdvertisme nt();//销毁点金广告
};
#endif
源文件CMAdvertisement.cpp
#include "CMAdvertisement.h"
void CMAdvertisement::getDianJinAdvertisement()
{
//判断当前是否为Android平台
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//定义Jni函数信息结构体
JniMethodInfo minfo;
//返回一个bool值表示是否找到此函数
bool isHave = JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/application/gameTeam/ApplicationDemo","addDianjin", "()V");
if (isHave) {
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
#endif
}
void CMAdvertisement::destroyDianJinAdvertisme nt()
{
//判断当前是否为Android平台
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
//定义Jni函数信息结构体
JniMethodInfo minfo;
//返回一个bool值表示是否找到此函数
bool isHave = JniHelper::getStaticMethodInfo(minfo,"org/cocos2dx/application/gameTeam/ApplicationDemo","destroyDianjin", "()V");
if (isHave) {
minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID);
}
#endif
}
说明一下getStaticMethodInfo方法里的第二个参数的意义是在cocos2d-x中调用的java中的类的路径,包括包名,第三个参数是调用java类里面的方法,对应的java里面的方法是static的,如果不是的话,在调用的时候用非静态的去调,第四个参数是java方法的参数和返回值类型。
3、下面给出java中的代码:
package org.cocos2dx.application.gameTeam;
import org.cocos2dx.lib.Cocos2dxActivity;
import org.cocos2dx.lib.Cocos2dxEditText;
import org.cocos2dx.lib.Cocos2dxGLSurfaceView;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import com.nd.dianjin.DianJinPlatform;
import com.nd.dianjin.OfferBanner;
import com.nd.dianjin.DianJinPlatform.TitleStyle;
import com.nd.dianjin.OfferBanner.AnimationType;
import com.nd.dianjin.OfferBanner.BackgroundStyle;
import com.nd.dianjin.listener.OfferWallStateListener;
import com.nd.dianjin.utility.BannerColorConfig;
public class ApplicationDemo extends Cocos2dxActivity {
private Cocos2dxGLSurfaceView mGLView;
private static FrameLayout lin;
private static LinearLayout container;
static Activity activity;
//放在oncreate函数前面
static Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what){
case 0:
addAdvertisement();
break;
case 1:
DianJinPlatform.destroy();
if(lin!=null)
{
lin.removeView(container);
}
break;
case 2:
break;
}
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the packageName,it's used to set the resource path
String packageName = getApplication().getPackageName();
super.setPackageName(packageName);
// setContentView(R.layout.helloworld_demo);
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
lin = (FrameLayout)inflater.inflate(R.layout.helloworld_demo, null);
mGLView = (Cocos2dxGLSurfaceView) lin
.findViewById(R.id.helloworld_gl_surfaceview);
mGLView.setTextField((Cocos2dxEditText) lin
.findViewById(R.id.textField));
// mGLView = (Cocos2dxGLSurfaceView)
// findViewById(R.id.helloworld_gl_surfaceview);
// mGLView.setTextField((Cocos2dxEditText)
// findViewById(R.id.textField));
activity = this;
//addAdvertisement();
setContentView(lin);
//addAdvertisement();
}
public static void addDianjin(){
Message msg = Message.obtain();
msg.what = 0;
ApplicationDemo.mHandler.sendMessage(msg);
}
public static void destroyDianjin(){
Message msg = Message.obtain();
msg.what = 1;
ApplicationDemo.mHandler.sendMessage(msg);
}
public static void addAdvertisement() {
DianJinPlatform.initialize(ApplicationDemo.activity, 1023,
"a7d3ccc9d223e88298e4d7bb 54d5f59f");
// setContentView(R.layout.dianjin_show_app_layout);
// LinearLayout container = (LinearLayout)
// findViewById(R.id.ad_container);
BannerColorConfig colorConfig = new BannerColorConfig();
container.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
colorConfig.setBackgroundColor(Color.BLUE);
container.setOrientation(LinearLayout.VERTICAL);
colorConfig.setDetailColor(Color.WHITE);
colorConfig.setNameColor(Color.WHITE);
colorConfig.setRewardColor(Color.WHITE);
colorConfig.setBackgroundStyle(BackgroundStyle.BLUE);
OfferBanner bannerLayout = new OfferBanner(ApplicationDemo.activity, 10000,
AnimationType.ANIMATION_PUSHLEFT, colorConfig);
container.setGravity(Gravity.CENTER_HORIZONTAL);
container.addView(bannerLayout);
Button earnMoney = new Button(ApplicationDemo.activity);
earnMoney.setText("小日本子");
earnMoney.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
container.addView(earnMoney);
// Button earnMoney = (Button) findViewById(R.id.make_money);
earnMoney.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
DianJinPlatform.showOfferWall(ApplicationDemo.activity,
DianJinPlatform.Oriention.SENSOR);
DianJinPlatform.showOfferWall(ApplicationDemo.activity,
DianJinPlatform.Oriention.SENSOR, TitleStyle.GREEN);
}
});
DianJinPlatform.setOfferWallStateListene r(new OfferWallStateListener() {
@Override
public void onOfferWallState(int code) {
switch (code) {
case DianJinPlatform.DIANJIN_OFFERWALL_START:
Toast.makeText(ApplicationDemo.activity, "推广墙开始",
Toast.LENGTH_SHORT).show();
break;
case DianJinPlatform.DIANJIN_OFFERWALL_DESTROY:
Toast.makeText(ApplicationDemo.activity, "推广墙结束",
Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
});
if(lin!=null)
{
lin.addView(container);
}
}
@Override
protected void onPause() {
super.onPause();
mGLView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLView.onResume();
//addAdvertisement();
}
static {
System.loadLibrary("box2d");
System.loadLibrary("cocosdenshion");
System.loadLibrary("helloworld");
}
}
4、需要注意的是:在cocos2d-x调用的时候,不能直接调用java里的方法,会造成线程的冲突,必须在oncreate函数前面增加Handler,并且必须是static先进行加载,利用Handler进行消息的传递,不会出现线程的lock。
static Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what){
case 0:
addAdvertisement();
break;
case 1:
DianJinPlatform.destroy();
if(lin!=null)
{
lin.removeView(container);
}
break;
case 2:
break;
}
}
};
先把dianjin.jar拷到libs下面,选择add jars就可以了,选择下面的add external jars在打包的时候会出现没有添加进去的现象。
6、目前到这里吧
转载地址:http://blog.sina.com.cn/s/blog_a229ad7f01010x9r.html