当前位置:网站首页 > 图文广告 > 正文

小米广告sdk集成指南

作者:admin发布时间:2021-09-02分类:图文广告浏览:评论:29


导读:-x-x项目中的代码(){;=(,"","","()Landroid/content/Context;");jobjectactivityObject;if(hasContext)...

小米广告sdk集成指南

-x

-x项目中的代码

(){;=(,"","","()Landroid/content/Context;");jobjectactivityObject;if(hasContext){activityObject=methodInfo.env-CallStaticObjectMethod(methodInfo.classID,methodInfo.methodID);}else{return;}//修改requestAd为Android项目里Activity中相应的函数名boolhasMethod=JniHelper::getMethodInfo(methodInfo,"org/cocos2dx/cpp/AppActivity","requestAd","(Ljava/lang/String;)V");if(hasMethod){//修改为相应的POSITION_IDjstringmsg=methodInfo.env-NewStringUTF(POSITION_ID);methodInfo.env-CallVoidMethod(activityObject,methodInfo.methodID,msg);}else{return;}}extern"C"{//修改adCallback为Android项目里Activity中相应的函数名JNIEXPORTvoidJNICALLJava_org_cocos2dx_cpp_AppActivity_adCallback(JNIEnv*env,jobjectinstance,jstringmsg_){constchar*msg=env-GetStringUTFChars(msg_,0);//TODOenv-ReleaseStringUTFChars(msg_,msg);}}

Android项目中的代码

publicvoidrequestGameVideoAd(finalStringPOSITION_ID){AdPuppetManager.requestGameVideoAd(UnityPlayerActivity.this,POSITION_ID,newAdListener(){@OverridepublicvoidonAdEvent(finalAdEventevent){adCallback("GameVideoAdadevent"+event.name());}@OverridepublicvoidonAdError(finalAdErrorerror){adCallback("GameVideoAdaderror:"+error.name());}@OverridepublicvoidonAdLoaded(){adCallback("GameVideoAdadload");}});}publicvoidrequestInterstitialAd(finalStringPOSITION_ID){AdPuppetManager.requestInterstitialAd(UnityPlayerActivity.this,POSITION_ID,newAdListener(){@OverridepublicvoidonAdEvent(finalAdEventevent){adCallback("InterstitialAdadevent"+event.name());}@OverridepublicvoidonAdError(finalAdErrorerror){adCallback("InterstitialAdaderror:"+error.name());}@OverridepublicvoidonAdLoaded(){adCallback("InterstitialAdadload");}});}//此方法应在收到requestGameVideoAd参数中的AdListener的onAdLoaded()的回调后调用,达到预加载的效果publicvoidshowGameVideoAd(finalStringPOSITION_ID){AdPuppetManager.showGameVideoAd(POSITION_ID);}//此方法应在收到requestInterstitialAd参数中的AdListener的onAdLoaded()的回调后调用,达到预加载的效果publicvoidshowInterstitialAd(finalStringPOSITION_ID){if(AdPuppetManager.isInterstitialAdReady(POSITION_ID)){AdPuppetManager.showInterstitialAd(POSITION_ID);}}publicnativevoidadCallback(Stringmsg);

Unity3D

Unity3D项目中的代码

usingUnityEngine;usingSystem.Collections;usingSystem.Runtime.InteropServices;publicclassUnityRequestAd:MonoBehaviour{AndroidJavaClassandroidJavaClass;AndroidJavaObjectandroidJavaObject;voidStart(){androidJavaObject=null;androidJavaClass=newAndroidJavaClass("com.unity3d.player.UnityPlayer");}voidOnGUI(){if(GUI.Button(newRect(100,100,500,200),"AdTestButton")){if(androidJavaClass!=null){androidJavaObject=androidJavaClass.GetStaticAndroidJavaObject("currentActivity");}else{return;}if(androidJavaObject!=null){//修改requestAd为Android项目里Activity中对应的函数名//修改为相应的POSITION_IDandroidJavaObject.Call("requestAd","POSITION_ID");}else{return;}}}//修改adCallback为Android项目里Activity中相应的函数名voidadCallback(stringmsg){print(msg);//TODO}}

Android项目中的代码

publicvoidrequestGameVideoAd(finalStringPOSITION_ID){AdPuppetManager.requestGameVideoAd(UnityPlayerActivity.this,POSITION_ID,newAdListener(){@OverridepublicvoidonAdEvent(finalAdEventevent){UnityPlayer.UnitySendMessage("MainCamera","adCallback","GameVideoAdadevent:"+event.name());}@OverridepublicvoidonAdError(finalAdErrorerror){UnityPlayer.UnitySendMessage("MainCamera","adCallback","GameVideoAdaderror:"+error.name());}@OverridepublicvoidonAdLoaded(){UnityPlayer.UnitySendMessage("MainCamera","adCallback","GameVideoAdadload");}});}publicvoidrequestInterstitialAd(finalStringPOSITION_ID){AdPuppetManager.requestInterstitialAd(UnityPlayerActivity.this,POSITION_ID,newAdListener(){@OverridepublicvoidonAdEvent(finalAdEventevent){UnityPlayer.UnitySendMessage("MainCamera","adCallback","InterstitialAdadevent:"+event.name());}@OverridepublicvoidonAdError(finalAdErrorerror){UnityPlayer.UnitySendMessage("MainCamera","adCallback","InterstitialAdaderror:"+error.name());}@OverridepublicvoidonAdLoaded(){UnityPlayer.UnitySendMessage("MainCamera","adCallback","InterstitialAdadload");}});}//此方法应在收到requestGameVideoAd参数中的AdListener的onAdLoaded()的回调后调用,达到预加载的效果publicvoidshowGameVideoAd(finalStringPOSITION_ID){AdPuppetManager.showGameVideoAd(POSITION_ID);}//此方法应在收到requestInterstitialAd参数中的AdListener的onAdLoaded()的回调后调用,达到预加载的效果publicvoidshowInterstitialAd(finalStringPOSITION_ID){if(AdPuppetManager.isInterstitialAdReady(POSITION_ID)){AdPuppetManager.showInterstitialAd(POSITION_ID);}}

4.FAQ

Q:如何实现预加载?A:对于插屏类型广告和游戏视屏类型广告,请求广告的回调里有个onLoaded(),此函数表示广告加载完毕,收到此回调后随时可以调用显示广告的方法。

Q:如何判断是否可以展示广告?A:可以使用AdPuppetManager.isInterstitialAdReady(POSITION_ID)判断插屏广告是否可以展示,AdPuppetManager.isGameVideoReady(POSITION_ID)判断游戏视屏广告是否可以展示。

Q:cocos2D为什么无法加载?A:如果用自带的命令打包会把asserts里的文件给删掉,建议在eclipse或者AndroidStudio来打包,保证成功率和方便我们进行技术支持。

Q:为什么视频暂停了(见下图)?

A:本sdk的视频与UnityAD的视频有冲突,如果在同一个app里会导致此情况的发生。

Q:为什么无法加载?A:建议先看看Demo,仍旧无法解决请找我们的技术支持。

文档版本:0.3.9

最后更新日期:2016-10-08


已有29位网友发表了看法:

欢迎 发表评论: