cocos2d-x 植入广告(二) 有米横屏、插屏广告和积分墙广告

相比于百度广告的SDK,有米广告的植入就很简单了,因为有米提供的有cocos2d-x的demo,基本上就是看着文档的流程,照着demo写自己的代码。

【申请发布ID和应用密钥,下载SDK】

1.上传应用,获取有米提供的发布ID和应用密钥

2.在有米官网下载cocos2d-x的demo

3.将自己项目的android版本导入到eclipse中



准备工作做好了,现在开始正式操作了。


【导入有米提供的jar,配置】

在下载的demo中找到YoumiSdk_v5.00_2015-01-08.jar文件,将它导入到自己项目的libs文件夹中



之后右键选择 Build Path 再选择 Add to Build Path ,将jar导入到项目中。



完成之后,下图红圈内会有显示



接下来,配置AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.richard.Zodiac"	<!-- 自己的包名 -->
      android:versionCode="1"
      android:versionName="1.0"
      android:installLocation="auto">

    <uses-sdk android:minSdkVersion="9"/>
    <uses-feature android:glEsVersion="0x00020000" />

    <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.GET_TASKS" />
	<!-- 以下为可选权限 -->
	<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
	<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    
    <application android:label="@string/app_name"
                 android:icon="@drawable/icon">
					 
        <!-- Tell Cocos2dxActivity the name of our .so -->
        <meta-data android:name="android.app.lib_name"
	              android:value="cocos2dcpp" />

        <activity android:name="org.cocos2dx.cpp.AppActivity"
                  android:label="@string/app_name"
                  android:screenOrientation="portrait"
                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
                  android:configChanges="orientation">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
                 <!-- 有米必备组件配置 -->
        <activity
            android:name="net.youmi.android.AdBrowser"
            android:configChanges="keyboard|keyboardHidden|orientation|screenSize"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Light.NoTitleBar" >
        </activity>

        <service
            android:name="net.youmi.android.AdService"
            android:exported="false" >
        </service>

        <receiver android:name="net.youmi.android.AdReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.PACKAGE_ADDED" />

                <data android:scheme="package" />
            </intent-filter>
        </receiver>

        <!-- 有米积分广告所需组件 -->
        <service
            android:name="net.youmi.android.ExpService"
            android:exported="false" >
        </service>

        <!-- (可选)设置有米广告推广渠道号(这里替换为非负整数的渠道号),参数列表:http://wiki.youmi.net/Wiki/PromotionChannelIDs -->
        <meta-data
            android:name="YOUMI_CHANNEL"
            android:value="10360" >		<!-- 上线渠道号 -->
        </meta-data>
        
    </application>

    <supports-screens android:anyDensity="true"
                      android:smallScreens="true"
                      android:normalScreens="true"
                      android:largeScreens="true"
                      android:xlargeScreens="true"/>

    <uses-permission android:name="android.permission.INTERNET"/>
</manifest> 

这里需要注意,渠道号要对应上线渠道。比如我在腾讯上线,渠道号是10360。


之后就是代码了,先写Java代码,再写C++代码。


有米提供三种广告形式,Banner广告,插屏广告,还有积分墙。


实际操作中可以选择其中的一种或几种,这里,我选择全部使用


【Java代码】

/****************************************************************************
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 net.youmi.android.AdManager;
import net.youmi.android.banner.AdSize;
import net.youmi.android.banner.AdView;
import net.youmi.android.banner.AdViewListener;
import net.youmi.android.listener.Interface_ActivityListener;
import net.youmi.android.offers.OffersManager;
import net.youmi.android.offers.OffersWallDialogListener;
import net.youmi.android.offers.PointsChangeNotify;
import net.youmi.android.offers.PointsManager;
import net.youmi.android.spot.SpotDialogListener;
import net.youmi.android.spot.SpotManager;

import org.cocos2dx.lib.Cocos2dxActivity;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.Toast;

public class AppActivity extends Cocos2dxActivity implements PointsChangeNotify {

	// 以下常量为各种功能的标识,值随意起
	private final static int SHOW_SPOT_AD = 100;

	private final static int SHOW_BANNER = 102;

	private final static int HIDE_BANNER = 103;

	private final static int SHOW_OFFER_WALL = 200;

	private final static int SHOW_OFFER_WALL_DIALOG = 201;

	private final static int QUERY_POINTS = 202;

	private final static int AWARD_POINTS = 203;

	private final static int SPEND_POINTS = 204;

	/**
	 * 无积分Banner
	 */
	private AdView mBannerView;

	private LinearLayout mBannerLayout;

	private static Handler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// 初始化接口,应用启动的时候调用,参数:appId,appSecret,是否开启调试模式
		AdManager.getInstance(this).init("f7d12635a71a5015","b97bb08fe2496d5e",false);

		// (可选)开启用户数据统计服务,(sdk v4.08之后新增功能)默认不开启,传入false值也不开启,只有传入true才会调用
		AdManager.getInstance(this).setUserDataCollect(true);

		// --------------------------------------------------------------------------------
		// 积分广告初始化及相关设置
		// 如果使用积分广告,请务必调用积分广告的初始化接口:
		OffersManager.getInstance(this).onAppLaunch();

		// (可选)注册积分监听-随时随地获得积分的变动情况
		PointsManager.getInstance(this).registerNotify(this);

		// --------------------------------------------------------------------------------
		// 插屏接口初始化及相关设置
		// (建议使用)预加载插播资源
		SpotManager.getInstance(this).loadSpotAds();

		// (可选) 设置插屏图片为横屏
		SpotManager.getInstance(this).setSpotOrientation(
				SpotManager.ORIENTATION_PORTRAIT);

		// (可选) 设置插屏广告动画效果为高级动画效果
		SpotManager.getInstance(this).setAnimationType(SpotManager.ANIM_ADVANCE);

		// --------------------------------------------------------------------------------
		initHandler();

	}

	@Override
	protected void onDestroy() {
		// (可选)注销积分监听
		// 如果在onCreate调用了PointsManager.getInstance(this).registerNotify(this)进行积分余额监听器注册,那这里必须得注销
		PointsManager.getInstance(this).unRegisterNotify(this);

		// 回收积分广告占用的资源
		OffersManager.getInstance(this).onAppExit();

		// 回收插屏广告占用的资源
		SpotManager.getInstance(this).onDestroy();
		super.onDestroy();
	}

	@Override
	protected void onStop() {
		// 如果不调用此方法,则按home键的时候会出现图标无法显示的情况。
		SpotManager.getInstance(this).onStop();
		super.onStop();
	}

	private void initHandler() {
		handler = new Handler() {

			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub

				switch (msg.what) {
				case SHOW_SPOT_AD:
					// 展示插屏广告
					SpotManager.getInstance(AppActivity.this).showSpotAds(
							AppActivity.this,new SpotDialogListener() {

								@Override
								public void onShowSuccess() {
									//showTipsInUiThread("插屏广告展示成功",Toast.LENGTH_SHORT);
								}

								@Override
								public void onShowFailed() {
									//showTipsInUiThread("插屏广告展示失败\n原因请查看Logcat-tag:YoumiSdk",Toast.LENGTH_LONG);
								}

								@Override
								public void onSpotClosed() {
									//showTipsInUiThread("插屏广告关闭了",Toast.LENGTH_SHORT);
								}

							});
					break;
				case SHOW_BANNER:
					if (mBannerView == null) {

						// 实例化广告条
						mBannerView = new AdView(AppActivity.this,AdSize.FIT_SCREEN);
						mBannerView.setAdListener(new AdViewListener() {

							@Override
							public void onSwitchedAd(AdView arg0) {
								//showTipsInUiThread("广告条切换广告了",Toast.LENGTH_SHORT);
							}

							@Override
							public void onReceivedAd(AdView arg0) {
								//showTipsInUiThread("广告条接收到广告了",Toast.LENGTH_SHORT);
							}

							@Override
							public void onFailedToReceivedAd(AdView arg0) {
								//showTipsInUiThread("广告条展示失败",Toast.LENGTH_LONG);
							}
						});
						// 创建布局来承载广告条
						if (mBannerLayout == null) {
							mBannerLayout = new LinearLayout(AppActivity.this);
							mBannerLayout.setLayoutParams(new LinearLayout.LayoutParams(
									LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
							mBannerLayout.addView(mBannerView);
						}
						// 采用WindowManager来进行
						WindowManager mWindowManager = (WindowManager) getContext().getSystemService(
								Context.WINDOW_SERVICE);
						WindowManager.LayoutParams mWmParams = new WindowManager.LayoutParams();
						mWmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
								| WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
						mWmParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
						mWmParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
						mWmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
						mWmParams.alpha = 1.0F;
						mWmParams.format = 1;
						mWmParams.gravity = Gravity.BOTTOM | Gravity.RIGHT; // 这里示例为:在右下角展示广告条条
						mWindowManager.addView(mBannerLayout,mWmParams);
					}
					break;
				case HIDE_BANNER:
					if (mBannerLayout != null) {
						((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE))
								.removeView(mBannerLayout);
						mBannerLayout = null;
						mBannerView = null;
					} else {
						//showTipsInUiThread("当前广告条没有展示",Toast.LENGTH_SHORT);
					}
					break;
				case SHOW_OFFER_WALL:
					// 展示积分墙全屏对话框
					OffersManager.getInstance(AppActivity.this).showOffersWall(
							new Interface_ActivityListener() {

								@Override
								public void onActivityDestroy(Context context) {
									// TODO Auto-generated method stub
									//showTipsInUiThread("全屏积分墙退出了",Toast.LENGTH_LONG);
								}
							});
					break;
				case SHOW_OFFER_WALL_DIALOG:
					// 积分墙对话框支持多种重载,详情请查看文档
					OffersManager.getInstance(AppActivity.this).showOffersWallDialog(
							AppActivity.this,new OffersWallDialogListener() {

								@Override
								public void onDialogClose() {
									//showTipsInUiThread("积分墙对话框关闭了",Toast.LENGTH_SHORT);
								}
							});
					break;

				default:
					break;
				}

				super.handleMessage(msg);
			}
		};
	}

	/**
	 * 展示插屏广告
	 */
	public static void showSpotAd() {
		sendMsgToHandler(SHOW_SPOT_AD);
	}

	/**
	 * 关闭插屏广告
	 * 
	 * @return true : 什么都不做<br>
	 *         false : 执行开发者写好的关闭插屏广告后的逻辑
	 */
	public static boolean closeSpotAd() {
		return SpotManager.getInstance(getContext()).disMiss();
	}

	/**
	 * 展示广告条
	 */
	public static void showBanner() {
		sendMsgToHandler(SHOW_BANNER);
	}

	/**
	 * 隐藏广告条
	 */
	public static void hideBanner() {
		sendMsgToHandler(HIDE_BANNER);
	}

	/**
	 * 展示全屏积分墙
	 */
	public static void showOfferWall() {
		sendMsgToHandler(SHOW_OFFER_WALL);
	}

	/**
	 * 展示对话框积分墙
	 */
	public static void showOfferWallDialog() {
		sendMsgToHandler(SHOW_OFFER_WALL_DIALOG);
	}

	/**
	 * 查询积分
	 * 
	 * @return 整形积分值
	 */
	public static int queryPoints() {
		return PointsManager.getInstance(getContext()).queryPoints();
	}

	/**
	 * 奖励积分
	 * 
	 * @param points
	 *            奖励的积分
	 * @return 操作是否成功
	 */
	public static boolean awardPoints(int points) {
		return PointsManager.getInstance(getContext()).awardPoints(points);
	}

	/**
	 * 消耗积分
	 * 
	 * @param points
	 *            消耗的积分
	 * @return 操作是否成功
	 */
	public static boolean spendPoints(int points) {
		return PointsManager.getInstance(getContext()).spendPoints(points);
	}

	/**
	 * 积分余额变动通知,当用户的积分发生变动(增加或者减少)时,会回调本方法,本方法执行在UI线程中
	 */
	@Override
	public void onPointBalanceChange(int points) {
		// TODO Auto-generated method stub
		//showTipsInUiThread("积分余额发生变动了,当前积分:" + points,Toast.LENGTH_SHORT);

		// 积分增加或者减少,可以通过下面的函数在C++进行一些操作,如提示积分不足等。
		pointsBalanceChange(points);
	}

	/**
	 * 定义JAVA本地函数,通过本地函数,调用C++的代码
	 * 
	 * @param point
	 * @return
	 */
	private native int pointsBalanceChange(int points);

	private synchronized static void sendMsgToHandler(int type) {
		Message msg = handler.obtainMessage();
		msg.what = type;
		msg.sendToTarget();
	}

	public static void showTipsInUiThread(final String str,final int duartion) {
		handler.post(new Runnable() {

			@Override
			public void run() {
				Toast.makeText(getContext(),str,duartion).show();
			}
		});
	}
}


代码都是从demo中copy过来的,需要注意的是

1.

AdManager.getInstance(this).init("f7d12635a71a5015",false);
这里要使用自己在有米获取的 发布ID和应用密钥


2.

public static void showTipsInUiThread(final String str,duartion).show();
			}
		});
	}

这个函数在测试的时候用着很方便,发布的时候可以把它去下来。

【C++部分】

通过jni实现C++层和Java层互相调用,其原理可以查看官网文档

回到C++层,在我们项目的Classes目录下,自定义一个类,这里我建了名为AdHelp.h和AdHelp.cpp的文件


AdHelp.h

/*
************************************************************************
*
*	AdHelp.h
*	Author: 
*       describe: 插广告
*
************************************************************************
*/

#ifndef __AdHelp__H__
#define __AdHelp__H__

#include <cocos2d.h>
using namespace cocos2d;

#if(CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)

#include "platform/android/jni/JniHelper.h"
#include <jni.h>
#include <android/log.h>

#endif

class AdHelp
{
public:

	AdHelp();
	virtual ~AdHelp();

	// -------------------------------------------------------------
	// 无积分广告使用
	static void showSpotAd();           //通过JNI调用JAVA静态函数,实现展示插屏广告
	static bool closeSpotAd();          //通过JNI调用JAVA静态函数,实现关闭插屏广告
	static void showBanner();           //通过JNI调用JAVA静态函数,实现展示广告条
	static void hideBanner();           //通过JNI调用JAVA静态函数,实现隐藏广告条

	// -------------------------------------------------------------
	// 积分广告使用
	static void showOfferWall();        //通过JNI调用JAVA静态函数,实现展示积分墙(全屏)
	static void showOfferWallDialog();  //通过JNI调用JAVA静态函数,实现展示积分墙(对话框)
	static int queryPoints();           //通过JNI调用JAVA静态函数,实现查询当前积分
	static bool awardPoints(int points);//通过JNI调用JAVA静态函数,实现增加积分
	static bool spendPoints(int points);//通过JNI调用JAVA静态函数,实现消耗积分
};

#endif


AdHelp.cpp

/*
************************************************************************
*
*	AdHelp.cpp
*	Author: 
*       describe: 插广告
*
************************************************************************
*/

#include "AdHelp.h"

AdHelp::AdHelp(){}
AdHelp::~AdHelp(){}

// -------------------------------------------------------------------------------------------------------------------------------------
// 无积分广告调用

/**
 * 展示插屏广告(C++ 通过JNI 调用JAVA方法)
 */
void AdHelp::showSpotAd()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    
    // 定义JNI函数信息结构体
    JniMethodInfo showSpotAd;
    
    /**
     * @param 第一个参数:JNI函数信息结构体
     * @param 第二个参数:与你Android项目中的主Activity所在的包名对应,Demo中为:com.youmi.android.cocos2dx.demo.MainActivity,需要改为com/youmi/android/cocos2dx/denmo/MainActivity
     * @param 第三个参数:Android项目中对应的Activity里面所定义的JAVA静态函数名,Demo中为:showSpotAd
     * @param 第四个参数:该JAVA方法所需要传入的值,格式为"(JAVA方法中需要传入的值)c++中本函数返回值",Demo中,showSpotAd的JAVA方法不需要传入值,而YoumiAd::showSpotAd()返回值为void,所以这里写 "()V" 
     * @return 返回bool值表示是否找到静态函数
     */
    bool isHave = JniHelper::getStaticMethodInfo(showSpotAd,"org/cocos2dx/cpp/AppActivity","showSpotAd","()V"); 
    if (!isHave) {
        CCLog("jni:showSpotAd此函数不存在");
    }else{
        CCLog("jni:showSpotAd函数存在");
        showSpotAd.env->CallStaticVoidMethod(showSpotAd.classID,showSpotAd.methodID);
    }
	#endif
}

/**
 * 关闭插屏广告(C++ 通过JNI 调用JAVA方法)
 */
bool AdHelp::closeSpotAd()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    JniMethodInfo closeSpotAd;
    bool isHave = JniHelper::getStaticMethodInfo(closeSpotAd,"closeSpotAd","()Z"); 
    if (!isHave) {
        CCLog("jni:closeSpotAd此函数不存在");
        return false;
    }else{
        CCLog("jni:closeSpotAd函数存在");
        return closeSpotAd.env->CallStaticBooleanMethod(closeSpotAd.classID,closeSpotAd.methodID);
    }
	#endif

	return true;
}


/**
 * 展示广告条(C++ 通过JNI 调用JAVA方法)
 */
void AdHelp::showBanner()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台

    // 定义JNI函数信息结构体
    JniMethodInfo showBanner;
    
    /**
     * @param 第一个参数:JNI函数信息结构体
     * @param 第二个参数:与你Android项目中的主Activity所在的包名对应,Demo中为:com.youmi.android.cocos2dx.demo.MainActivity,需要改为com/youmi/android/cocos2dx/denmo/MainActivity
     * @param 第三个参数:Android项目中对应的Activity里面所定义的JAVA静态函数名,Demo中为:showBanner
     * @param 第四个参数:该JAVA方法所需要传入的值,格式为"(JAVA方法中需要传入的值)c++中本函数返回值",Demo中,showBanner的JAVA方法不需要传入值,而YoumiAd::showBanner()返回值为void,所以这里写 "()V" 
     * @return 返回bool值表示是否找到静态函数
     */
    bool isHave = JniHelper::getStaticMethodInfo(showBanner,"showBanner","()V"); 
    if (!isHave) {
        CCLog("jni:showBanner此函数不存在");
    }else{
        CCLog("jni:showBanner函数存在");
        showBanner.env->CallStaticVoidMethod(showBanner.classID,showBanner.methodID);
    }
	#endif
}
 
/**
 * 隐藏广告条(C++ 通过JNI 调用JAVA方法)
 */
void AdHelp::hideBanner()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    JniMethodInfo hideBanner;
    bool isHave = JniHelper::getStaticMethodInfo(hideBanner,"hideBanner","()V"); 
    if (!isHave) {
        CCLog("jni:hideBanner此函数不存在");
    }else{
        CCLog("jni:hideBanner函数存在");
        hideBanner.env->CallStaticVoidMethod(hideBanner.classID,hideBanner.methodID);
    }
	#endif
}

// -------------------------------------------------------------------------------------------------------------------------------------
// 积分广告调用
/**
 * 展示积分墙(C++ 通过JNI 调用JAVA方法)
 */
void AdHelp::showOfferWall()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    
    // 定义JNI函数信息结构体
    JniMethodInfo showOfferWall;
    
    /**
     * @param 第一个参数:JNI函数信息结构体
     * @param 第二个参数:与你Android项目中的主Activity所在的包名对应,Demo中为:com.youmi.android.cocos2dx.demo.MainActivity,需要改为com/youmi/android/cocos2dx/denmo/MainActivity
     * @param 第三个参数:Android项目中对应的Activity里面所定义的JAVA静态函数名,Demo中为:showOfferWall
     * @param 第四个参数:该JAVA方法所需要传入的值,格式为"(JAVA方法中需要传入的值)c++中本函数返回值",Demo中,showOfferWall的JAVA方法不需要传入值,而YoumiAd::showOfferWall()返回值为void,所以这里写 "()V" 
     * @return 返回bool值表示是否找到静态函数
     */
    bool isHave = JniHelper::getStaticMethodInfo(showOfferWall,"showOfferWall","()V"); 
 
    if (!isHave) {
        CCLog("jni:showOfferWall此函数不存在");
    }else{
        CCLog("jni:showOfferWall函数存在");
        showOfferWall.env->CallStaticVoidMethod(showOfferWall.classID,showOfferWall.methodID);
    }
	#endif
}

/**
 * 展示积分墙对话框(C++ 通过JNI 调用JAVA方法)
 */
void AdHelp::showOfferWallDialog()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    JniMethodInfo showOfferWallDialog;
    bool isHave = JniHelper::getStaticMethodInfo(showOfferWallDialog,"showOfferWallDialog","()V"); 
 
    if (!isHave) {
        CCLog("jni:showOfferWallDialog此函数不存在");
    }else{
        CCLog("jni:showOfferWalDialogl函数存在");
        showOfferWallDialog.env->CallStaticVoidMethod(showOfferWallDialog.classID,showOfferWallDialog.methodID);
    }
	#endif
}

/**
 * 查询当前积分(C++ 通过JNI 调用JAVA方法)
 */
int AdHelp::queryPoints()
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    JniMethodInfo queryPoints;
    bool isHave = JniHelper::getStaticMethodInfo(queryPoints,"queryPoints","()I"); 
    if (!isHave) {
        CCLog("jni:queryPoints此函数不存在");
        return -1;
    }else{
        CCLog("jni:queryPoints函数存在");
		return queryPoints.env->CallStaticIntMethod(queryPoints.classID,queryPoints.methodID);
    }
	#endif

	return true;
}

/**
 * 奖励积分 (C++ 通过JNI 调用JAVA方法)
 */
bool AdHelp::awardPoints(int points)
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    JniMethodInfo awardPoints;
    bool isHave = JniHelper::getStaticMethodInfo(awardPoints,"awardPoints","(I)Z"); 
 
    if (!isHave) {
        CCLog("jni:awardPoints此函数不存在");
        return false;
    }else{
        CCLog("jni:awardPoints此函数存在");
        return awardPoints.env->CallStaticBooleanMethod(awardPoints.classID,awardPoints.methodID,points);
    }
	#endif

	return true;
}

/**
 * 消耗积分 (C++ 通过JNI 调用JAVA方法)
 */
bool AdHelp::spendPoints(int points)
{
	#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) //判断当前是否为Android平台
    JniMethodInfo spendPoints;
    bool isHave = JniHelper::getStaticMethodInfo(spendPoints,"spendPoints","(I)Z"); 
 
    if (!isHave) {
        CCLog("jni:spendPoints此函数不存在");
        return false;
    }else{
        CCLog("jni:spendPoints此函数存在");
        return spendPoints.env->CallStaticBooleanMethod(spendPoints.classID,spendPoints.methodID,points);
    }
	#endif

	return true;
}


好啦,完工!


之后在合适的位置调用AdHelp中相应的静态函数就行了。


最后不要忘了把新建的AdHelp.cpp和Classes目录下的其它类一样,也放在jni目录下的Android.mk 中。


重新编译运行,如果不出意外,就可以看到有米广告了。


Banner广告 插屏广告 积分墙



上线地址

91手机助手 百度助手

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...