1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
| import android.content.Intent; import android.graphics.Color; import android.graphics.drawable.ColorDrawable; import android.support.annotation.MainThread; import android.util.DisplayMetrics; import android.util.Log; import android.view.View; import android.widget.FrameLayout; import android.widget.Toast;
import com.bytedance.sdk.openadsdk.AdSlot; import com.bytedance.sdk.openadsdk.TTAdConfig; import com.bytedance.sdk.openadsdk.TTAdConstant; import com.bytedance.sdk.openadsdk.TTAdManager; import com.bytedance.sdk.openadsdk.TTAdNative; import com.bytedance.sdk.openadsdk.TTAdSdk; import com.bytedance.sdk.openadsdk.TTAppDownloadListener; import com.bytedance.sdk.openadsdk.TTNativeExpressAd; import com.bytedance.sdk.openadsdk.TTRewardVideoAd; import com.bytedance.sdk.openadsdk.TTSplashAd;
import java.util.List;
public class AdService { private static final String TAG = "AdService"; private static AdService adService = null;
private String mVerticalCodeId = "945020931"; private TTAdNative mTTAdNative; private TTRewardVideoAd mttRewardVideoAd; private boolean mHasShowDownloadActive = false;
private String mExpressCodeId = "945052853"; private View mNativeExpressView = null; private float mNativeExpressViewY = 0; private float mNativeExpressViewX = 0; private boolean mNativeExpressViewCanShow = false;
//是否强制跳转到主页面 private boolean mForceGoMain; //开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms private static final int AD_TIME_OUT = 3000; private String mCodeId = "887299625"; private boolean mIsExpress = false; //是否请求模板广告
public static AdService getInstance(){ if (adService == null){ adService = new AdService(); } return adService; }
public void init(AppActivity _activity){ activity = _activity;
TTAdConfig config = new TTAdConfig.Builder() .appId("5046871") .useTextureView(true) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景,可以使用TextureView .appName("dddd") .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK) .allowShowNotify(true) //是否允许sdk展示通知栏提示 .allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页 .debug(false) //测试阶段打开,可以通过日志排查问题,上线时去除该调用 .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_3G) //允许直接下载的网络状态集合 .supportMultiProcess(true)//是否支持多进程 .needClearTaskReset() //.httpStack(new MyOkStack3())//自定义网络库,demo中给出了okhttp3版本的样例,其余请自行开发或者咨询工作人员。 .build(); TTAdSdk.init(_activity.getApplicationContext(), config); TTAdManager ttAdManager = TTAdSdk.getAdManager(); //step2:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。 TTAdSdk.getAdManager().requestPermissionIfNecessary(_activity); //step3:创建TTAdNative对象,用于调用广告请求接口 mTTAdNative = ttAdManager.createAdNative(_activity.getApplicationContext()); loadAd(mVerticalCodeId, TTAdConstant.VERTICAL); }
/** * 加载开屏广告 */ public void loadSplashAd() { //step3:创建开屏广告请求参数AdSlot,具体参数含义参考文档 AdSlot adSlot = null; if (mIsExpress) { //个性化模板广告需要传入期望广告view的宽、高,单位dp,请传入实际需要的大小, //比如:广告下方拼接logo、适配刘海屏等,需要考虑实际广告大小 float expressViewWidth = UIUtils.getScreenWidthDp(activity); float expressViewHeight = UIUtils.getHeight(activity); adSlot = new AdSlot.Builder() .setCodeId(mCodeId) .setSupportDeepLink(true) .setImageAcceptedSize(1080, 1920) //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告,请在穿山甲平台查看 .setExpressViewAcceptedSize(expressViewWidth, expressViewHeight) .build(); } else { adSlot = new AdSlot.Builder() .setCodeId(mCodeId) .setSupportDeepLink(true) .setImageAcceptedSize(1080, 1920) .build(); }
//step4:请求广告,调用开屏广告异步请求接口,对请求回调的广告作渲染处理 mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() { @Override @MainThread public void onError(int code, String message) { Log.i(TAG, String.valueOf(message)); showToast(message); goToMainActivity(); }
@Override @MainThread public void onTimeout() { showToast("开屏广告加载超时"); goToMainActivity(); }
@Override @MainThread public void onSplashAdLoad(TTSplashAd ad) { Log.i(TAG, "开屏广告请求成功"); if (ad == null) { return; } //获取SplashView View view = ad.getSplashView(); Log.i(TAG, "" + activity.isFinishing()); if (view != null && activity.mSplashContainer != null) { activity.mSplashContainer.removeAllViews(); //把SplashView 添加到ViewGroup中,注意开屏广告view:width >=70%屏幕宽;height >=50%屏幕高 activity.mSplashContainer.addView(view); //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置,您需要自定义倒计时逻辑 //ad.setNotAllowSdkCountdown(); }else { goToMainActivity(); }
//设置SplashView的交互监听器 ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() { @Override public void onAdClicked(View view, int type) { Log.i(TAG, "onAdClicked"); showToast("开屏广告点击"); }
@Override public void onAdShow(View view, int type) { Log.i(TAG, "onAdShow"); showToast("开屏广告展示"); }
@Override public void onAdSkip() { Log.i(TAG, "onAdSkip"); showToast("开屏广告跳过"); goToMainActivity();
}
@Override public void onAdTimeOver() { Log.i(TAG, "onAdTimeOver"); showToast("开屏广告倒计时结束"); goToMainActivity(); } }); if(ad.getInteractionType() == TTAdConstant.INTERACTION_TYPE_DOWNLOAD) { ad.setDownloadListener(new TTAppDownloadListener() { boolean hasShow = false;
@Override public void onIdle() { }
@Override public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { if (!hasShow) { showToast("下载中..."); hasShow = true; } }
@Override public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { showToast("下载暂停...");
}
@Override public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { showToast("下载失败...");
}
@Override public void onDownloadFinished(long totalBytes, String fileName, String appName) { showToast("下载完成...");
}
@Override public void onInstalled(String fileName, String appName) { showToast("安装完成...");
} }); } } }, AD_TIME_OUT);
}
public void loadExpressAd(int y) { //mExpressContainer.removeAllViews();
mNativeExpressViewY = y; mNativeExpressViewX = (1080 - 900) / 2; if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView); float expressViewWidth = UIUtils.px2dip(activity,900); float expressViewHeight = UIUtils.px2dip(activity,800); //Log.i("!!!!!!!!!!!!!!!!!", "loadExpressAd y:" + y + " x:" + mNativeExpressViewX + " w:" + expressViewWidth + " h:" + expressViewHeight); //step4:创建广告请求参数AdSlot,具体参数含义参考文档 AdSlot adSlot = new AdSlot.Builder() .setCodeId(mExpressCodeId) //广告位id .setSupportDeepLink(true) .setAdCount(1) //请求广告数量为1到3条 .setExpressViewAcceptedSize(expressViewWidth,expressViewHeight) //期望模板广告view的size,单位dp .build(); //step5:请求广告,对请求回调的广告作渲染处理 mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() { @Override public void onError(int code, String message) { showToast("load error : " + code + ", " + message); //mExpressContainer.removeAllViews(); if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView); }
@Override public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) { if (ads == null || ads.size() == 0){ return; } TTNativeExpressAd mTTAd = ads.get(0); mTTAd.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() { @Override public void onAdClicked(View view, int type) { showToast("广告被点击"); }
@Override public void onAdShow(View view, int type) { showToast("广告展示"); }
@Override public void onRenderFail(View view, String msg, int code) { //Log.e("ExpressView","render fail:"+(System.currentTimeMillis() - startTime)); showToast(msg+" code:"+code); }
@Override public void onRenderSuccess(View view, float width, float height) { //Log.e("ExpressView","render suc:"+(System.currentTimeMillis() - startTime)); //返回view的宽高 单位 dp showToast("渲染成功"); //mExpressContainer.removeAllViews(); if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView); mNativeExpressView = null; if(!mNativeExpressViewCanShow) return; mNativeExpressView = view; mNativeExpressView.setY(mNativeExpressViewY); mNativeExpressView.setX(mNativeExpressViewX); // mNativeExpressView.setBackground(new ColorDrawable(Color.WHITE)); //FrameLayout.LayoutParams linearParams = (FrameLayout.LayoutParams) mNativeExpressView.getLayoutParams(); //linearParams.width = 900; //linearParams.height = 507; //mNativeExpressView.setLayoutParams(linearParams); activity.mFrameLayout.addView(view); } }); mTTAd.render(); } }); } /** * 跳转到主页面 */ private void goToMainActivity() {
activity.mSplashContainer.removeAllViews(); activity.setContentView(activity.mFrameLayout);
loadAd(mVerticalCodeId, TTAdConstant.VERTICAL); }
private void showToast(String msg) { Toast.makeText(activity, msg, Toast.LENGTH_LONG); }
/** * AT_Banner_Top = 1,//顶上 banner; * AT_Banner_Bottom,//底部 banner; * AT_Interstitial,//插屏广告; * AT_RewardVideo,//视频广告; * @param type */ public void showAd(int type){ Log.i("!!!!!!!!!!!!!!!!!", "showRewardVideoAd showAd type:" + type); if(type == -1){ // 關閉插屏廣告 if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView); }else if(type == -2){ // 恢复插屏廣告 if(mNativeExpressView != null) { activity.mFrameLayout.removeView(mNativeExpressView); activity.mFrameLayout.addView(mNativeExpressView); } }else if(type == -3){ // 彻底關閉插屏廣告 if(mNativeExpressView != null) activity.mFrameLayout.removeView(mNativeExpressView); mNativeExpressView = null; mNativeExpressViewCanShow = false; }else if(type < 0){ // 打開插屏廣告 mNativeExpressViewCanShow = true; loadExpressAd(-type); }else{ showRewardVideoAd(); } }
private void showRewardVideoAd(){
//Log.i("!!!!!!!!!!!!!!!!!", "showRewardVideoAd"); if (mttRewardVideoAd != null) { //step6:在获取到广告后展示 //该方法直接展示广告 mttRewardVideoAd.showRewardVideoAd(activity);
//展示广告,并传入广告展示的场景 //mttRewardVideoAd.showRewardVideoAd(activity,TTAdConstant.RitScenes.CUSTOMIZE_SCENES,"scenes_test"); mttRewardVideoAd = null; } else { loadAd(mVerticalCodeId, TTAdConstant.VERTICAL); PushJniService.onVideoAdReward(0,4, "1"); } } private void loadAd(String codeId, int orientation) { //Log.i("!!!!!!!!!!!!!!!!!", "loadAd"); //step4:创建广告请求参数AdSlot,具体参数含义参考文档 // AdSlot adSlot = new AdSlot.Builder() // .setCodeId(codeId) // .setSupportDeepLink(true) // .setRewardName("金币") //奖励的名称 // .setRewardAmount(3) //奖励的数量 // .setUserID("user123")//用户id,必传参数 // .setMediaExtra("media_extra") //附加参数,可选 // .setOrientation(orientation) //必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL // .build(); AdSlot adSlot = new AdSlot.Builder() .setCodeId(codeId) .setSupportDeepLink(true) .setAdCount(1) .setImageAcceptedSize(1080, 1920) .setRewardName("金币") //奖励的名称 .setRewardAmount(3) //奖励的数量 .setUserID("user123")//用户id,必传参数 .setMediaExtra("media_extra") //附加参数,可选 .setOrientation(orientation) //必填参数,期望视频的播放方向:TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL .build(); //step5:请求广告 mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() { @Override public void onError(int code, String message) { Log.i("Toast", message); }
//视频广告加载后,视频资源缓存到本地的回调,在此回调后,播放本地视频,流畅不阻塞。 @Override public void onRewardVideoCached() { Log.i("Toast", "rewardVideoAd video cached"); }
//视频广告的素材加载完毕,比如视频url等,在此回调后,可以播放在线视频,网络不好可能出现加载缓冲,影响体验。 @Override public void onRewardVideoAdLoad(TTRewardVideoAd ad) { Log.i("Toast", "rewardVideoAd loaded 广告类型:"); mttRewardVideoAd = ad; mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {
@Override public void onAdShow() { Log.i("Toast", "rewardVideoAd show"); }
@Override public void onAdVideoBarClick() { Log.i("Toast", "rewardVideoAd bar click"); }
@Override public void onAdClose() { Log.i("Toast", "rewardVideoAd close"); activity.runOnGLThread(new Runnable() { @Override public void run() { PushJniService.onVideoAdReward(1,4, "1"); } }); }
//视频播放完成回调 @Override public void onVideoComplete() { Log.i("Toast", "rewardVideoAd complete"); loadAd(mVerticalCodeId, TTAdConstant.VERTICAL); }
@Override public void onVideoError() { Log.i("Toast", "rewardVideoAd error"); activity.runOnGLThread(new Runnable() { @Override public void run() { PushJniService.onVideoAdReward(0,4, "1"); } }); }
//视频播放完成后,奖励验证回调,rewardVerify:是否有效,rewardAmount:奖励梳理,rewardName:奖励名称 @Override public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName) { Log.i("Toast", "verify:" + rewardVerify + " amount:" + rewardAmount + " name:" + rewardName); }
@Override public void onSkippedVideo() { Log.i("Toast", "rewardVideoAd has onSkippedVideo"); activity.runOnGLThread(new Runnable() { @Override public void run() { PushJniService.onVideoAdReward(0,4, "1"); } }); } }); mttRewardVideoAd.setDownloadListener(new TTAppDownloadListener() { @Override public void onIdle() { mHasShowDownloadActive = false; }
@Override public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) { if (!mHasShowDownloadActive) { mHasShowDownloadActive = true; Log.i("Toast", "下载中,点击下载区域暂停"); } }
@Override public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) { Log.i("Toast", "下载暂停,点击下载区域继续"); }
@Override public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) { Log.i("Toast", "下载失败,点击下载区域重新下载"); }
@Override public void onDownloadFinished(long totalBytes, String fileName, String appName) { Log.i("Toast", "下载完成,点击下载区域重新下载"); }
@Override public void onInstalled(String fileName, String appName) { Log.i("Toast", "安装完成,点击下载区域打开"); } }); } }); } }
|