方式一:
1. 在cocos2dx编译完成的项目(proj.android)加入 GDTUnionSDK.4.8.524.min.jar广点通jar包。
2. 添加权限声明
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
3.添加activity, service的声明
<service android:name="com.qq.e.comm.DownloadService" android:exported="false" /> <activity <span style="white-space:pre"> </span>android:name="com.qq.e.ads.ADActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize" />4. 在AppActivity中添加广点通demo中显示插屏广告的方法, 并且添加一个静态的Handler成员变量.
<span style="white-space:pre"> </span>private static Handler mHandler; private InterstitialAD iad; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mHandler = new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: showAD(); break; case 2: showAsPopup(); break; case 3: closeAsPopup(); break; default: break; } }; }; } private InterstitialAD getIAD() { if (iad == null) { iad = new InterstitialAD(this,Constants.APPID,Constants.InterteristalPosID); } return iad; } private void showAD() { getIAD().setADListener(new AbstractInterstitialADListener() { @Override public void onNoAD(int arg0) { Log.i("longer","LoadInterstitialAd Fail:" + arg0); } @Override public void onADReceive() { Log.i("longer","onADReceive"); iad.show(); } }); iad.loadAD(); } private void showAsPopup() { getIAD().setADListener(new AbstractInterstitialADListener() { @Override public void onNoAD(int arg0) { Log.i("longer","LoadInterstitialAd Fail:" + arg0); } @Override public void onADReceive() { iad.showAsPopupWindow(); } }); iad.loadAD(); } private void closeAsPopup() { iad.closePopupWindow(); }4. 添加静态方法, 提供给c++调用
<span style="white-space:pre"> </span>/** * c++中調用的方法 * @param what 1: 使用showAD方式展示插屏广告 * 2: 使用showAsPopup方式展示广告 * 3: 关闭popup广告 */ public static void showAd(int what) { mHandler.sendEmptyMessage(what); }
5. 在cocos2dx工程中添加GDTAD.h和GDTAD.cpp文件,类容如下:
.h文件
#ifndef GDTAD_H #define GDTAD_H class GDTAD { public: /* what 1 -- 10 插屏广告, 1 : 显示 what 11 -- 20 ......, 11: 显示 what 21 -- 30 ......, 21: 显示 */ static void showAd(int what); }; #endif //GDTAD_Hcpp文件
#include "GDTAD.h" #include "cocos2d.h" USING_NS_CC; #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "platform/android/jni/JniHelper.h" #include <jni.h> const char* AppActivityCalssName = "org/cocos2dx/cpp/AppActivity"; void GDTAD::showAd(int what) { cocos2d::JniMethodInfo jniShowAd; if (cocos2d::JniHelper::getStaticMethodInfo(jniShowAd,AppActivityCalssName,"showAd","(I)V")) { jniShowAd.env->CallStaticVoidMethod(jniShowAd.classID,jniShowAd.methodID,what); } else { log("jni:showAD false"); } } #else void GDTAD::showAd(int what) { log("jni:showAD"); return; } #endif
6. 在想要显示广告的地方调用GDTAD类的静态方法showAd进行广告的显示。
GDTAD::showAd(1);
7. 集成效果如app: http://pan.baidu.com/s/1dFK5Uk5 原文链接:https://www.f2er.com/cocos2dx/339606.html