当前位置:   article > 正文

Android 接入穿山甲SDK之开屏广告_com.bytedance.sdk.openadsdk

com.bytedance.sdk.openadsdk

大家可以先参考我的上一篇博客介绍了如何集成SDK以及一些工具类传送门

首先创建一个脚本写入如下内容

  1. package com.unity3d.player.chuanshanjia;
  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Build;
  5. import android.os.Bundle;
  6. import android.text.TextUtils;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.widget.FrameLayout;
  10. import androidx.annotation.Nullable;
  11. import com.bytedance.sdk.openadsdk.AdSlot;
  12. import com.bytedance.sdk.openadsdk.TTAdConstant;
  13. import com.bytedance.sdk.openadsdk.TTAdNative;
  14. import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
  15. import com.bytedance.sdk.openadsdk.TTSplashAd;
  16. import com.unity3d.player.R;
  17. import com.unity3d.player.UnityPlayerActivity;
  18. import com.unity3d.player.tools.ChuanShanJiaUtil;
  19. import com.unity3d.player.tools.TToast;
  20. public class CsjSplashActivity extends Activity {
  21. private static final String TAG = "SplashActivity";
  22. private TTAdNative mTTAdNative;
  23. private FrameLayout mSplashContainer;
  24. //是否强制跳转到主页面
  25. private boolean mForceGoMain;
  26. //开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
  27. private static final int AD_TIME_OUT = 4000;
  28. private String mCodeId = "开屏广告位ID";
  29. private boolean mIsExpress = false; //是否请求模板广告
  30. private boolean mIsHalfSize = false;
  31. private static CsjSplashActivity _Instance;
  32. public static CsjSplashActivity Inst(){
  33. return _Instance;
  34. }
  35. @SuppressWarnings("RedundantCast")
  36. @Override
  37. protected void onCreate(@Nullable Bundle savedInstanceState) {
  38. super.onCreate(savedInstanceState);
  39. _Instance = this;
  40. setContentView(R.layout.activity_splash);
  41. mSplashContainer = (FrameLayout) findViewById(R.id.splash_container);
  42. //step2:创建TTAdNative对象
  43. mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
  44. hideBottomUIMenu(); //隐藏虚拟按键,并且全屏
  45. getExtraInfo();
  46. TTAdManagerHolder.Inst().init(this,"应用ID","应用昵称");
  47. }
  48. private void getExtraInfo() {
  49. Intent intent = getIntent();
  50. if(intent == null) {
  51. return;
  52. }
  53. String codeId = intent.getStringExtra("splash_rit");
  54. if (!TextUtils.isEmpty(codeId)){
  55. mCodeId = codeId;
  56. }
  57. mIsExpress = intent.getBooleanExtra("is_express", false);
  58. mIsHalfSize = intent.getBooleanExtra("is_half_size", false);
  59. }
  60. @Override
  61. protected void onResume() {
  62. //判断是否该跳转到主页面
  63. if (mForceGoMain) {
  64. goToMainActivity();
  65. }
  66. super.onResume();
  67. }
  68. @Override
  69. protected void onStop() {
  70. super.onStop();
  71. mForceGoMain = true;
  72. }
  73. /**
  74. * 加载开屏广告
  75. */
  76. public void loadSplashAd(){
  77. AdSlot adSlot=null;
  78. float splashWidthDp = ChuanShanJiaUtil.getScreenWidthDp(this);
  79. int splashWidthPx = ChuanShanJiaUtil.getScreenWidthInPx(this);
  80. int screenHeightPx = ChuanShanJiaUtil.getScreenHeight(this);
  81. float screenHeightDp = ChuanShanJiaUtil.px2dip(this, screenHeightPx);
  82. float splashHeightDp;
  83. int splashHeightPx;
  84. if (mIsHalfSize) {
  85. // 开屏高度 = 屏幕高度 - 下方预留的高度,demo中是预留了屏幕高度的1/5,因此开屏高度传入 屏幕高度*4/5
  86. splashHeightDp = screenHeightDp * 4/5.f;
  87. splashHeightPx = (int) (screenHeightPx * 4/5.f);
  88. } else {
  89. splashHeightDp = screenHeightDp;
  90. splashHeightPx = screenHeightPx;
  91. }
  92. if (mIsExpress) {
  93. //个性化模板广告需要传入期望广告view的宽、高,单位dp,请传入实际需要的大小,
  94. //比如:广告下方拼接logo、适配刘海屏等,需要考虑实际广告大小
  95. // float expressViewWidth = ChuanShanJiaUtil.getScreenWidthDp(this);
  96. // float expressViewHeight = ChuanShanJiaUtil.getHeight(this);
  97. adSlot = new AdSlot.Builder()
  98. .setCodeId(mCodeId)
  99. .setSupportDeepLink(true)
  100. .setImageAcceptedSize(splashWidthPx, splashHeightPx)
  101. //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告,请在穿山甲平台查看
  102. .setExpressViewAcceptedSize(splashWidthDp, splashHeightDp)
  103. .build();
  104. } else {
  105. adSlot = new AdSlot.Builder()
  106. .setCodeId(mCodeId)
  107. .setSupportDeepLink(true)
  108. .setImageAcceptedSize(splashWidthPx, splashHeightPx)
  109. .build();
  110. }
  111. //step4:请求广告,调用开屏广告异步请求接口,对请求回调的广告作渲染处理
  112. mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
  113. @Override
  114. public void onError(int code, String message) {
  115. Log.d(TAG,"loadSplashAd OnError"+ message+" code:"+code);
  116. showToast(message);
  117. goToMainActivity();
  118. }
  119. @Override
  120. public void onTimeout() {
  121. Log.d(TAG,"loadSplashAd onTimeout");
  122. showToast("开屏广告加载超时");
  123. goToMainActivity();
  124. }
  125. @Override
  126. public void onSplashAdLoad(com.bytedance.sdk.openadsdk.TTSplashAd ad) {
  127. Log.d(TAG,"loadSplashAd success");
  128. Log.d(TAG, "开屏广告请求成功");
  129. if (ad == null) {
  130. return;
  131. }
  132. //获取SplashView
  133. View view = ad.getSplashView();
  134. if (view != null && mSplashContainer != null && !CsjSplashActivity.this.isFinishing()) {
  135. mSplashContainer.removeAllViews();
  136. //把SplashView 添加到ViewGroup中,注意开屏广告view:width >=70%屏幕宽;height >=50%屏幕高
  137. mSplashContainer.addView(view);
  138. //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置,您需要自定义倒计时逻辑
  139. //ad.setNotAllowSdkCountdown();
  140. }else {
  141. goToMainActivity();
  142. }
  143. //设置SplashView的交互监听器
  144. ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
  145. @Override
  146. public void onAdClicked(View view, int type) {
  147. Log.d(TAG, "onAdClicked");
  148. showToast("开屏广告点击");
  149. }
  150. @Override
  151. public void onAdShow(View view, int type) {
  152. Log.d(TAG, "onAdShow");
  153. showToast("开屏广告展示");
  154. }
  155. @Override
  156. public void onAdSkip() {
  157. Log.d(TAG, "onAdSkip");
  158. showToast("开屏广告跳过");
  159. goToMainActivity();
  160. }
  161. @Override
  162. public void onAdTimeOver() {
  163. Log.d(TAG, "onAdTimeOver");
  164. showToast("开屏广告倒计时结束");
  165. goToMainActivity();
  166. }
  167. });
  168. if(ad.getInteractionType() == TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
  169. ad.setDownloadListener(new TTAppDownloadListener() {
  170. boolean hasShow = false;
  171. @Override
  172. public void onIdle() {
  173. }
  174. @Override
  175. public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
  176. if (!hasShow) {
  177. showToast("下载中...");
  178. hasShow = true;
  179. }
  180. }
  181. @Override
  182. public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
  183. showToast("下载暂停...");
  184. }
  185. @Override
  186. public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
  187. showToast("下载失败...");
  188. }
  189. @Override
  190. public void onDownloadFinished(long totalBytes, String fileName, String appName) {
  191. showToast("下载完成...");
  192. }
  193. @Override
  194. public void onInstalled(String fileName, String appName) {
  195. showToast("安装完成...");
  196. }
  197. });
  198. }
  199. }
  200. },AD_TIME_OUT);
  201. }
  202. /**
  203. * 跳转到主页面
  204. */
  205. private void goToMainActivity(){
  206. Intent intent=new Intent(CsjSplashActivity.this, UnityPlayerActivity.class);
  207. startActivity(intent);
  208. mSplashContainer.removeAllViews(); //移除所有视图
  209. this.finish();
  210. }
  211. private void showToast(String msg) {
  212. TToast.show(this, msg);
  213. }
  214. /**
  215. * 隐藏虚拟按键,并且全屏
  216. */
  217. protected void hideBottomUIMenu() {
  218. //隐藏虚拟按键,并且全屏
  219. if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
  220. View v = this.getWindow().getDecorView();
  221. v.setSystemUiVisibility(View.GONE);
  222. } else if (Build.VERSION.SDK_INT >= 19) {
  223. //for new api versions.
  224. View decorView = getWindow().getDecorView();
  225. int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
  226. | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
  227. decorView.setSystemUiVisibility(uiOptions);
  228. }
  229. }
  230. }

脚本创建完毕之后在AndroidMainfest文件中写入如下代码

  1. <activity
  2. android:name="com.unity3d.player.chuanshanjia.CsjSplashActivity"
  3. android:configChanges="keyboard|orientation|screenSize"
  4. android:screenOrientation="sensorLandscape"
  5. android:theme="@style/Theme.Splash">
  6. <!-- android:theme="@android:style/Theme.Black">-->
  7. <intent-filter>
  8. <action android:name="android.intent.action.MAIN" />
  9. <!--表示在点击app图标的时候启动此Activity-->
  10. <category android:name="android.intent.category.LAUNCHER" />
  11. </intent-filter>
  12. </activity>

Theme是在项目里面的res/values目录下创建了一个名为themes.xml具体内容如下

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources xmlns:tools="http://schemas.android.com/tools">
  3. <style name="Theme.Splash" parent="Theme.Light">
  4. <item name="android:statusBarColor" tools:ignore="NewApi">#FF000000</item>
  5. </style>
  6. <style name="Theme.Light" parent="Theme.AppCompat.Light">
  7. <!-- <item name="android:windowBackground">@color/default_window_bg</item>-->
  8. <item name="android:windowTranslucentNavigation" tools:ignore="NewApi">true</item>
  9. <!--解决部分手机隐藏状态栏顶部出现小黑条的问题-->
  10. <item name="android:windowLayoutInDisplayCutoutMode" tools:ignore="NewApi">shortEdges</item>
  11. <item name="android:windowFullscreen">true</item>
  12. </style>
  13. </resources>

下一篇 插屏广告 传送门

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/322977
推荐阅读
相关标签
  

闽ICP备14008679号