Bootstrap

CocosCreator接入穿山甲广告2-插屏广告

看过上一篇文章的小伙伴,我这里就不在进行对初始化等进行墨迹,直接上插屏广告的逻辑,一般在游戏比如关卡结束的时候会弹出插屏广告。
穿山甲sdk初始化后,在oncreate方法里直接调用chuanShanJiaInsertAd这个方法就可以,然后在你需要掉用插屏的时候调用下oadInsertAd这个方法就ok

public void chuanShanJiaInsertAd(){
     //step2:创建TTAdNative对象,用于调用广告请求接口,createAdNative(Context context) 插屏广告context需要传入Activity对象
     mTTInsertAdNative = TTAdManagerHolder.get().createAdNative(getApplicationContext());
     //step3:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
     TTAdManagerHolder.get().requestPermissionIfNecessary(this);
 }
 //加载插屏广告  
 public static void loadInsertAd(final String Id,final String adType){
     float expressViewWidth = 350;
     float expressViewHeight = 500;
     //step4:创建广告请求参数AdSlot,具体参数含义参考文档
     AdSlot adSlot = new AdSlot.Builder()
             .setCodeId("广告位id") //
             .setSupportDeepLink(true)
             .setAdCount(1) //请求广告数量为1到3条
             .setExpressViewAcceptedSize(expressViewWidth,expressViewHeight) //期望模板广告view的size,单位dp
             .setImageAcceptedSize(350 ,500 )//这个参数设置即可,不影响模板广告的size
             .build();
     //step5:请求广告,对请求回调的广告作渲染处理
     mTTInsertAdNative.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
         @Override
         public void onError(int code, String message) {
             //TToast.show(InteractionExpressActivity.this, "load error : " + code + ", " + message);
             Log.e("插屏广告请求错误",message);
         }

         @Override
         public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
             if (ads == null || ads.size() == 0){
                 return;
             }
             mTTAd = ads.get(0);
             bindAdListener(mTTAd);
             //startTime = System.currentTimeMillis();
             mTTAd.render();
         }
     });
 }
// private long startTime = 0;
 private static boolean mHasShowDownloadActive1 = false;
 private static void bindAdListener(TTNativeExpressAd ad) {
     ad.setExpressInteractionListener(new TTNativeExpressAd.AdInteractionListener() {
         @Override
         public void onAdDismiss() {
             //TToast.show(mContext, "广告关闭");
         }

         @Override
         public void onAdClicked(View view, int type) {
             //TToast.show(mContext, "广告被点击");
         }

         @Override
         public void onAdShow(View view, int type) {
             //TToast.show(mContext, "广告展示");
             Log.e("插屏广告显示","onAdShow");
         }

         @Override
         public void onRenderFail(View view, String msg, int code) {
             //Log.e("ExpressView","render fail:"+(System.currentTimeMillis() - startTime));
             //TToast.show(mContext, msg+" code:"+code);
         }

         @Override
         public void onRenderSuccess(View view, float width, float height) {
             //Log.e("ExpressView","render suc:"+(System.currentTimeMillis() - startTime));
             //返回view的宽高 单位 dp
             //TToast.show(mContext, "渲染成功");
             Log.e("插屏广告渲染成功","successful");
             mTTAd.showInteractionExpressAd(Instance);

         }
     });

     if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
         return;
     }
     ad.setDownloadListener(new TTAppDownloadListener() {
         @Override
         public void onIdle() {
             //TToast.show(InteractionExpressActivity.this, "点击开始下载", Toast.LENGTH_LONG);
         }

         @Override
         public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
             if (!mHasShowDownloadActive1) {
                 mHasShowDownloadActive1 = true;
                 //TToast.show(InteractionExpressActivity.this, "下载中,点击暂停", Toast.LENGTH_LONG);
             }
         }

         @Override
         public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
             //TToast.show(InteractionExpressActivity.this, "下载暂停,点击继续", Toast.LENGTH_LONG);
         }

         @Override
         public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
            // TToast.show(InteractionExpressActivity.this, "下载失败,点击重新下载", Toast.LENGTH_LONG);
         }

         @Override
         public void onInstalled(String fileName, String appName) {
             //TToast.show(InteractionExpressActivity.this, "安装完成,点击图片打开", Toast.LENGTH_LONG);
         }

         @Override
         public void onDownloadFinished(long totalBytes, String fileName, String appName) {
             //TToast.show(InteractionExpressActivity.this, "点击安装", Toast.LENGTH_LONG);
         }
     });
 }

如果小伙伴在激励视频接入成功后,那么插屏这个逻辑更简单,不过有点坑的是
TTAdNative.loadInteractionAd(AdSlot adSlot, @NonNull InteractionAdListener listener)文档中给的这个加载插屏广告的方法,当你调用后运行后你会发现加载不了插屏广告,error信息提示你“媒体请求素材是否原生与录入平台不一致”,如图
在这里插入图片描述
但实际上是这个api被废弃了 ,这问题也是蛮坑的,文档中没有说明当时也是花了我一点功夫…
" mTTInsertAdNative.loadInteractionExpressAd(adSlot, new TTAdNative.NativeExpressAdListener()"
现在使用这个方法加载插屏广告。插屏广告也介绍完,接下来就是比较坑的接入底部banner了,下篇文章介绍。

;