• Android项目集成穿山甲开屏/插屏/横幅广告教程大全


    Android项目集成穿山甲开屏/插屏/横幅广告教程大全

    开发及项目环境说明

    Android Studio 2020.3.1 Patch 4/203.7717.56.2031.7935034 jdk11.0.9

    Android Gradle Plugin Version 7.0.4 Gradle Version 7.0.2

    compileSdk 31 minSdk 21 targetSdk 31 穿山甲com.pangle.cn:ads-sdk-pro:4.4.0.9

    因为Gradle Version过高,官方的集成文档就很鸡肋老是报错,这里用全新的方式settings.gradle中集成。

    集成初始化SDK和工具类

    集成SDK

    Gradle Version 7.0.2版本较高的情况下使用这种方式,若比较低请参考官方接入文档。

    从穿山甲3.5.0.6版本开始,开发者也可以使用Gradle依赖导入穿山甲SDK。

    请勿在project级别的build.gradle文件中添加Maven的引用。要在settings.gradle文件中添加Maven的引用,url ‘https://artifact.bytedance.com/repository/pangle’

    示例:

    dependencyResolutionManagement {
        repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
        repositories {
            google()
            mavenCentral()
            //下面3行添加在这里
            maven {
                url 'https://artifact.bytedance.com/repository/pangle'
            }
        }
    }
    rootProject.name = "项目名"
    include ':app'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    添加依赖

    需要在主modulebuild.gradle文件添加SDK依赖。

    dependencies {
        implementation 'com.pangle.cn:ads-sdk-pro:4.4.0.9' //这里用20220628的最新版
    }
    
    • 1
    • 2
    • 3

    AndroidManifest配置

    添加权限

    <!-- 必要权限 -->
        <uses-permission android:name="android.permission.INTERNET" /> <!-- 必要权限,解决安全风险漏洞,发送和注册广播事件需要调用带有传递权限的接口 -->
        <permission
            android:name="${applicationId}.openadsdk.permission.TT_PANGOLIN"
            android:protectionLevel="signature" />
    
        <uses-permission android:name="${applicationId}.openadsdk.permission.TT_PANGOLIN" /> <!-- 可选权限 -->
        <uses-permission android:name="android.permission.READ_PHONE_STATE" />
        <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
        <uses-permission
            android:name="android.permission.WRITE_EXTERNAL_STORAGE"
            tools:ignore="ScopedStorage" />
        <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
        <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
        <uses-permission android:name="android.permission.GET_TASKS" /> <!-- 可选,穿山甲提供“获取地理位置权限”和“不给予地理位置权限,开发者传入地理位置参数”两种方式上报用户位置,两种方式均可不选,添加位置权限或参数将帮助投放定位广告 -->
        <!-- 请注意:无论通过何种方式提供给穿山甲用户地理位置,均需向用户声明地理位置权限将应用于穿山甲广告投放,穿山甲不强制获取地理位置信息 -->
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <!-- 如果视频广告使用textureView播放,请务必添加,否则黑屏 -->
        <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- 穿山甲3400版本新增:建议添加“query_all_package”权限,穿山甲将通过此权限在Android R系统上判定广告对应的应用是否在用户的app上安装,避免投放错误的广告,以此提高用户的广告体验。若添加此权限,需要在您的用户隐私文档中声明! -->
        <uses-permission
            android:name="android.permission.QUERY_ALL_PACKAGES"
            tools:ignore="QueryAllPackagesPermission" />
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    provider配置

    注意:

    (1)为不影响下载类型广告使用 无论APP处于任何阶段provider都需要在清单文件中正常配置

    (2)为不影响到广告的转化及收益 请务必在清单文件中配置xxx.TTMultiProvider

    (3)${applicationId} 必须与开发者包名保持一致,否则会引发崩溃问题
    适配Anroid7.0及以上如果您的应用需要在Anroid7.0及以上环境运行,请在AndroidManifest中添加如下代码:

    <provider
        android:name="com.bytedance.sdk.openadsdk.TTFileProvider"
        android:authorities="${applicationId}.TTFileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
       <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    在res/xml目录下,新建一个xml文件file_paths,在该文件中添加如下代码:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
       <!--为了适配所有路径可以设置 path = "." -->
       <external-path name="tt_external_root" path="." />
       <external-path name="tt_external_download" path="Download" />
       <external-files-path name="tt_external_files_download" path="Download" />
       <files-path name="tt_internal_file_download" path="Download" />
       <cache-path name="tt_internal_cache_download" path="Download" />
    </paths>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    为了适配下载和安装相关功能,在工程中引用的包 com.android.support:support-v4:24.2.0使用24.2.0以及以上版本

    注意:单进程或多进程都必须配置

    <provider
        android:name="com.bytedance.sdk.openadsdk.multipro.TTMultiProvider"
        android:authorities="${applicationId}.TTMultiProvider"
        android:exported="false" />
    
    • 1
    • 2
    • 3
    • 4

    运行环境配置

    本项目 minSdkVersion="21" android:targetSdkVersion="31"

    代码混淆

    如果您需要使用proguard混淆代码,需确保不要混淆SDK的代码。 请在proguard.cfg文件(或其他混淆文件)尾部添加如下配置:

    官方说4000版本混淆规则:以aar包里的混淆文件为准我这里使用的是 ads-sdk-pro:4.4.0.9当前最新版就直接在文件proguard-rules.pro中添加混淆规则如下:

    -keep class com.bytedance.sdk.openadsdk.** { *; }
    -keep class com.bytedance.frameworks.** { *; }
    -keep class ms.bd.c.Pgl.**{*;}
    -keep class com.bytedance.mobsec.metasec.ml.**{*;}
    -keep class com.ss.android.**{*;}
    -keep class com.bytedance.embedapplog.** {*;}
    -keep class com.bytedance.embed_dr.** {*;}
    -keep class com.bykv.vk.** {*;}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    架构及白名单

    注意: 3900以及以上版本SDK默认支持armeabi-v7a,arm64-v8a两种架构(我项目中没有配置,直接使用了默认支持) ,如果有其他架构需求,请联系技术支持; 3900以下版本SDK中使用的so文件支持五种架构:x86,x86_64,armeabi,armeabi-v7a,arm64-v8a如果您应用中支持的架构超出这 五种,请在build.gradle中使用abiFilters选择支持的架构。如下所示:

    ndk { // 设置支持的 SO 库构架,注意这里要根据你的实际情况来设置 
       abiFilters  armeabi-v7a ,  arm64-v8a , x86 , x86_64 ,  armeabi 
    }
    
    • 1
    • 2
    • 3

    注意:平台SDK包中whiteList.txt 白名单上的资源不支持混淆

    在这里插入图片描述

    初始化SDK类

    配置完成之后创建一个SDK初始化类TTAdManagerHolder.java,初始化调用是在开屏界面调用,调用初始化成功之后会自动调用开屏代码,内容如下:

    import android.content.Context;
    import android.util.Log;
    
    import com.bytedance.sdk.openadsdk.TTAdConfig;
    import com.bytedance.sdk.openadsdk.TTAdConstant;
    import com.bytedance.sdk.openadsdk.TTAdManager;
    import com.bytedance.sdk.openadsdk.TTAdSdk;
    
    /**
     * 可以用一个单例来保存TTAdManager实例,在需要初始化sdk的时候调用
     */
    public class TTAdManagerHolder {
        private static final String TAG = "TTAdManagerHolder";
        private static boolean sInit;
        private static String appid = "这里写在穿山甲创建的应用id";//这里写在穿山甲创建的应用id
        private static String appName = "广告测试";
        private static TTAdManagerHolder _Instance;
    
        public static TTAdManager get() {
            return TTAdSdk.getAdManager();
        }
    
        public static TTAdManagerHolder Inst() {
            if (_Instance == null) {
                _Instance = new TTAdManagerHolder();
            }
            return _Instance;
        }
    
        public void init(final Context context) {
            doInit(context);
        }
    
        //step1:接入网盟广告sdk的初始化操作,详情见接入文档和穿山甲平台说明
        private void doInit(Context context) {
            if (!sInit) {
                TTAdSdk.init(context, buildConfig(context), new TTAdSdk.InitCallback() {
                    @Override
                    public void success() {
                        Log.i(TAG, "success: ");
    //                    SplashActivity.Inst().loadSplashAd();
                    }
    
                    @Override
                    public void fail(int code, String msg) {
                        Log.i(TAG, "fail:  code = " + code + " msg = " + msg);
                    }
                });
                sInit = true;
            }
        }
    
    
        private TTAdConfig buildConfig(Context context) {
            //强烈建议在应用对应的Application#onCreate()方法中调用,避免出现content为null的异常
            return new TTAdConfig.Builder()
                    .appId(appid)
                    .useTextureView(true) //默认使用SurfaceView播放视频广告,当有SurfaceView冲突的场景,可以使用TextureView
                    .appName(appName)
                    .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)//落地页主题
                    .allowShowNotify(true) //是否允许sdk展示通知栏提示,若设置为false则会导致通知栏不显示下载进度
                    .debug(true) //测试阶段打开,可以通过日志排查问题,上线时去除该调用
                    .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI) //允许直接下载的网络状态集合,没有设置的网络下点击下载apk会有二次确认弹窗,弹窗中会披露应用信息
                    .supportMultiProcess(false) //是否支持多进程,true支持
                    .asyncInit(true) //是否异步初始化sdk,设置为true可以减少SDK初始化耗时。3450版本开始废弃~~
                    //.httpStack(new MyOkStack3())//自定义网络库,demo中给出了okhttp3版本的样例,其余请自行开发或者咨询工作人员。
                    .build();
        }
    }
    
    
    • 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

    新建类MyApplication.java 用来做初始化,配置全局上下文

    import android.annotation.SuppressLint;
    import android.app.Application;
    import android.content.Context;
    
    public class MyApplication extends Application {
        @SuppressLint("StaticFieldLeak")
        private static Context mContext;
    
        public void onCreate() {
            super.onCreate();
            mContext = getApplicationContext();
            //初始化及设置开屏广告 参数 this
            new TTAdManagerHolder().init(this);
        }
    
        public static Context getContext() {
            return mContext;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    需要在此AndroidManifest.xml文件做配置即可:

        <application
            android:name=".MyApplication" <!-- 添加这行 -->
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:requestLegacyExternalStorage="true" <!-- 安卓高系统配置请求旧版外部存储 -->
            android:supportsRtl="true"
            android:theme="@style/Theme.CsjDemo"
            android:usesCleartextTraffic="true" <!--安卓高系统p配置使用明文流量 -->
            tools:targetApi="m">
            <activity
    		。。。。。。。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    注意: 穿山甲SDK不强制获取AndroidManifest.xml中权限,即使没有获取可选权限SDK也能正常运行;获取以上权限将帮助穿山甲优化投放广告精准度和用户的交互体验,提高eCPM。

    建议在广告请求前,合适的时机调用SDK提供的方法,在用户可以授权的情况下获取到声明中的权限,提高广告变现效率,申请通话、位置、存储权限,代码如下:

    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //获取到声明中的权限,提高广告变现效率。记得禁止插屏和Banner广告的权限申请否则会重复。
            TTAdManagerHolder.get().requestPermissionIfNecessary(MyApplication.getContext());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    SDK集成初始化完成之后开始接入SDK。

    一些工具类

    屏幕适配工具类

    UIUtils.java

    import android.app.Activity;
    import android.content.Context;
    import android.os.Build;
    import android.text.TextUtils;
    import android.util.DisplayMetrics;
    import android.view.Display;
    import android.view.DisplayCutout;
    import android.view.View;
    import android.view.ViewGroup;
    import android.view.ViewParent;
    import android.view.WindowInsets;
    import android.view.WindowManager;
    import android.widget.FrameLayout;
    import android.widget.LinearLayout;
    import android.widget.RelativeLayout;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    public class UIUtils {
        public static float getScreenWidthDp(Context context) {
            final float scale = context.getResources().getDisplayMetrics().density;
            float width = context.getResources().getDisplayMetrics().widthPixels;
            return width / (scale <= 0 ? 1 : scale) + 0.5f;
        }
    
        //全面屏、刘海屏适配
        public static float getHeight(Activity activity) {
            hideBottomUIMenu(activity);
            float height;
            int realHeight = getRealHeight(activity);
            if (UIUtils.hasNotchScreen(activity)) {
                height = px2dip(activity, realHeight - getStatusBarHeight(activity));
            } else {
                height = px2dip(activity, realHeight);
            }
            return height;
        }
    
        public static void hideBottomUIMenu(Activity activity) {
            if (activity == null) {
                return;
            }
            try {
                //隐藏虚拟按键,并且全屏
                if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
                    View v = activity.getWindow().getDecorView();
                    v.setSystemUiVisibility(View.GONE);
                } else if (Build.VERSION.SDK_INT >= 19) {
                    //for new api versions.
                    View decorView = activity.getWindow().getDecorView();
                    int uiOptions = View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION // hide nav bar
                            //                    | View.SYSTEM_UI_FLAG_FULLSCREEN // hide status bar
                            | View.SYSTEM_UI_FLAG_IMMERSIVE;
                    decorView.setSystemUiVisibility(uiOptions);
                    activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        //获取屏幕真实高度,不包含下方虚拟导航栏
        public static int getRealHeight(Context context) {
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics dm = new DisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                display.getRealMetrics(dm);
            } else {
                display.getMetrics(dm);
            }
            int realHeight = dm.heightPixels;
            return realHeight;
        }
    
        //获取状态栏高度
        public static float getStatusBarHeight(Context context) {
            float height = 0;
            int resourceId = context.getApplicationContext().getResources().getIdentifier("status_bar_height", "dimen", "android");
            if (resourceId > 0) {
                height = context.getApplicationContext().getResources().getDimensionPixelSize(resourceId);
            }
            return height;
        }
    
        public static int px2dip(Context context, float pxValue) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (pxValue / (scale <= 0 ? 1 : scale) + 0.5f);
        }
    
        public static int dp2px(Context context, float dp) {
            final float scale = context.getResources().getDisplayMetrics().density;
            return (int) (dp * scale + 0.5f);
        }
    
        /**
         * 判断是否是刘海屏
         *
         * @return
         */
        public static boolean hasNotchScreen(Activity activity) {
            return isAndroidPHasNotch(activity)
                    || getInt("ro.miui.notch", activity) == 1
                    || hasNotchAtHuawei(activity)
                    || hasNotchAtOPPO(activity)
                    || hasNotchAtVivo(activity);
        }
    
        /**
         * Android P 刘海屏判断
         *
         * @param activity
         * @return
         */
        public static boolean isAndroidPHasNotch(Activity activity) {
            boolean result = false;
            if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                DisplayCutout displayCutout = null;
                try {
                    WindowInsets windowInsets = activity.getWindow().getDecorView().getRootWindowInsets();
                    if (windowInsets != null) {
                        displayCutout = windowInsets.getDisplayCutout();
                    }
                    if (displayCutout != null) {
                        result = true;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    
        /**
         * 小米刘海屏判断.
         *
         * @return 0 if it is not notch ; return 1 means notch
         * @throws IllegalArgumentException if the key exceeds 32 characters
         */
        public static int getInt(String key, Activity activity) {
            int result = 0;
            if (isMiui()) {
                try {
                    ClassLoader classLoader = activity.getClassLoader();
                    @SuppressWarnings("rawtypes")
                    Class SystemProperties = classLoader.loadClass("android.os.SystemProperties");
                    //参数类型
                    @SuppressWarnings("rawtypes")
                    Class[] paramTypes = new Class[2];
                    paramTypes[0] = String.class;
                    paramTypes[1] = int.class;
                    Method getInt = SystemProperties.getMethod("getInt", paramTypes);
                    //参数
                    Object[] params = new Object[2];
                    params[0] = new String(key);
                    params[1] = new Integer(0);
                    result = (Integer) getInt.invoke(SystemProperties, params);
    
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } catch (NoSuchMethodException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
            }
            return result;
        }
    
        /**
         * 华为刘海屏判断
         *
         * @return
         */
        public static boolean hasNotchAtHuawei(Context context) {
            boolean ret = false;
            try {
                ClassLoader classLoader = context.getClassLoader();
                Class HwNotchSizeUtil = classLoader.loadClass("com.huawei.android.util.HwNotchSizeUtil");
                Method get = HwNotchSizeUtil.getMethod("hasNotchInScreen");
                ret = (boolean) get.invoke(HwNotchSizeUtil);
            } catch (ClassNotFoundException e) {
            } catch (NoSuchMethodException e) {
            } catch (Exception e) {
            } finally {
                return ret;
            }
        }
    
        public static final int VIVO_NOTCH = 0x00000020;//是否有刘海
        public static final int VIVO_FILLET = 0x00000008;//是否有圆角
    
        /**
         * VIVO刘海屏判断
         *
         * @return
         */
        public static boolean hasNotchAtVivo(Context context) {
            boolean ret = false;
            try {
                ClassLoader classLoader = context.getClassLoader();
                Class FtFeature = classLoader.loadClass("android.util.FtFeature");
                Method method = FtFeature.getMethod("isFeatureSupport", int.class);
                ret = (boolean) method.invoke(FtFeature, VIVO_NOTCH);
            } catch (ClassNotFoundException e) {
            } catch (NoSuchMethodException e) {
            } catch (Exception e) {
            } finally {
                return ret;
            }
        }
    
        /**
         * O-P-P-O刘海屏判断
         *
         * @return
         */
        public static boolean hasNotchAtOPPO(Context context) {
            String temp = "com.kllk.feature.screen.heteromorphism";
            String name = getKllkDecryptString(temp);
            return context.getPackageManager().hasSystemFeature(name);
        }
    
        public static boolean isMiui() {
            boolean sIsMiui = false;
            try {
                Class<?> clz = Class.forName("miui.os.Build");
                if (clz != null) {
                    sIsMiui = true;
                    //noinspection ConstantConditions
                    return sIsMiui;
                }
            } catch (Exception e) {
                // ignore
            }
            return sIsMiui;
        }
    
        /**
         * 用于o-p-p-o 版本隐私协议
         */
        public static String getKllkDecryptString(String encryptionString) {
    
            if (TextUtils.isEmpty(encryptionString)) {
                return "";
            }
            String decryptTag = "";
            String decryptCapitalized = "O" + "P" + "P" + "O";
            String decrypt = "o" + "p" + "p" + "o";
            if (encryptionString.contains("KLLK")) {
                decryptTag = encryptionString.replace("KLLK", decryptCapitalized);
            } else if (encryptionString.contains("kllk")) {
                decryptTag = encryptionString.replace("kllk", decrypt);
            }
            return decryptTag;
    
        }
    
        public static void setViewSize(View view, int width, int height) {
            if (view.getParent() instanceof FrameLayout) {
                FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) view.getLayoutParams();
                lp.width = width;
                lp.height = height;
                view.setLayoutParams(lp);
                view.requestLayout();
            } else if (view.getParent() instanceof RelativeLayout) {
                RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
                lp.width = width;
                lp.height = height;
                view.setLayoutParams(lp);
                view.requestLayout();
            } else if (view.getParent() instanceof LinearLayout) {
                LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) view.getLayoutParams();
                lp.width = width;
                lp.height = height;
                view.setLayoutParams(lp);
                view.requestLayout();
            }
        }
    
        public static int getScreenWidthInPx(Context context) {
            DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
            return dm.widthPixels;
        }
    
        public static int getScreenHeightInPx(Context context) {
            DisplayMetrics dm = context.getApplicationContext().getResources().getDisplayMetrics();
            return dm.heightPixels;
        }
    
        public static int getScreenHeight(Context context) {
            return (int) (getScreenHeightInPx(context) + getStatusBarHeight(context));
        }
    
        public static void removeFromParent(View view) {
            if (view != null) {
                ViewParent vp = view.getParent();
                if (vp instanceof ViewGroup) {
                    ((ViewGroup) vp).removeView(view);
                }
            }
        }
    
        /**
         * 获取全面屏宽高
         *
         * @param context
         * @return
         */
        public static int[] getScreenSize(Context context) {
            int[] size = new int[]{0, 0};
            if (context == null) {
                return size;
            }
            WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            Display display = windowManager.getDefaultDisplay();
            DisplayMetrics dm = new DisplayMetrics();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                display.getRealMetrics(dm);
            } else {
                display.getMetrics(dm);
            }
            size[0] = dm.widthPixels;
            size[1] = dm.heightPixels;
            return size;
        }
    }
    
    • 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

    弹窗提示工具类

    TToast.java

    import android.annotation.SuppressLint;
    import android.content.Context;
    import android.util.Log;
    import android.widget.Toast;
    
    public final class TToast {
        private static Toast sToast;
    
        public static void show(Context context, String msg) {
            show(context, msg, Toast.LENGTH_SHORT);
        }
    
        public static void show(Context context, String msg, int duration) {
            Toast toast = getToast(context);
            if (toast != null) {
                toast.setDuration(duration);
                toast.setText(String.valueOf(msg));
                toast.show();
            } else {
                Log.i("TToast", "toast msg: " + String.valueOf(msg));
            }
        }
    
        @SuppressLint("ShowToast")
        private static Toast getToast(Context context) {
            if (context == null) {
                return sToast;
            }
    //        if (sToast == null) {
    //            synchronized (TToast.class) {
    //                if (sToast == null) {
            sToast = Toast.makeText(context.getApplicationContext(), "", Toast.LENGTH_SHORT);
    //                }
    //            }
    //        }
            return sToast;
        }
    
        public static void reset() {
            sToast = null;
        }
    }
    
    
    • 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

    显示序列工具类

    ShowSeqUtils.java(激励视频广告使用)

    import android.os.Environment;
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    public class ShowSeqUtils {
        private FileReader fr;
        private BufferedReader br;
        private FileWriter fw;
        private BufferedWriter bw;
     
        public int loadShowSeq(){
            int show_seq = 1;
            //获取当前日期字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String data = sdf.format(new Date());
            //读取本地缓存show_seq的文件
            try {
                String rootPath =  Environment.getExternalStorageDirectory().getPath();
                File file = new File(rootPath + "/Android/data/com.snssdk.api/cache/adloadSeqTemp.txt");
                if (!file.exists()) {
                    file.createNewFile();
                    return show_seq;
                }
                fr = new FileReader(file);
                br = new BufferedReader(fr);
                String line = "";
                while ((line = br.readLine()) != null) {
                    String[] temp = line.split(",");
                    if (temp[0].equals(data)){
                        //日期相同返回字段
                        show_seq = Integer.parseInt(temp[1]);
                    }
                }
                return show_seq;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(fr!=null){
                        fr.close();
                    }
                    if(br!=null){
                        br.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return show_seq;
        }
     
        public void writeToFile(int show_seq){
            //获取当前日期字符串
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String data = sdf.format(new Date());
            String content = data+","+show_seq;
            //读取本地缓存show_seq的文件
            try {
                String rootPath =  Environment.getExternalStorageDirectory().getPath();
                File file = new File(rootPath + "/Android/data/com.snssdk.api/cache/");
                if (!file.exists()) {
                    file.mkdir();
                }
                String filename = file.getAbsolutePath()+"/adloadSeqTemp.txt";
                file = new File(filename);
                if(!file.exists()){
                    file.createNewFile();
                }
                fw = new FileWriter(file, false);
                fw.write(content);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if(fw!=null){
                        fw.flush();
                        fw.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
    • 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

    穿山甲之开屏广告

    首先创建一个安卓Activity界面,文件名为SplashActivity.java做开屏界面,写入如下内容:
    在这里插入图片描述

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Build;
    import android.os.Bundle;
    import android.text.TextUtils;
    import android.util.Log;
    import android.view.View;
    import android.widget.FrameLayout;
    import androidx.annotation.Nullable;
    import com.bytedance.sdk.openadsdk.AdSlot;
    import com.bytedance.sdk.openadsdk.TTAdConstant;
    import com.bytedance.sdk.openadsdk.TTAdNative;
    import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
    import com.bytedance.sdk.openadsdk.TTSplashAd;
    import com.yrj.csjdemo.Utils.TToast;
    import com.yrj.csjdemo.Utils.UIUtils;
    
    @SuppressLint("CustomSplashScreen")
    public class SplashActivity extends Activity {
        private static final String TAG = "SplashActivity";
        private TTAdNative mTTAdNative;
        private FrameLayout mSplashContainer;
        //是否强制跳转到主页面
        private boolean mForceGoMain;
    
        //开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
        private static final int AD_TIME_OUT = 3000;
        private String mCodeId = "这里写开屏广告代码位id";//开屏广告代码位id
        private boolean mIsExpress = false; //是否请求模板广告
        private boolean mIsHalfSize = false;
        private static SplashActivity _Instance;
    
        public static SplashActivity Inst() {
            return _Instance;
        }
    
        @SuppressWarnings("RedundantCast")
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            _Instance = this;
            setContentView(R.layout.activity_splash);
            mSplashContainer = (FrameLayout) findViewById(R.id.splash_container);
            //step2:创建TTAdNative对象
            mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
            hideBottomUIMenu();  //隐藏虚拟按键,并且全屏
            getExtraInfo();
            loadSplashAd();
        }
    
        private void getExtraInfo() {
            Intent intent = getIntent();
            if (intent == null) {
                return;
            }
            String codeId = intent.getStringExtra("splash_rit");
            if (!TextUtils.isEmpty(codeId)) {
                mCodeId = codeId;
            }
            mIsExpress = intent.getBooleanExtra("is_express", false);
            mIsHalfSize = intent.getBooleanExtra("is_half_size", false);
        }
    
        @Override
        protected void onResume() {
            //判断是否该跳转到主页面
            if (mForceGoMain) {
                goToMainActivity();
            }
            super.onResume();
        }
    
        @Override
        protected void onStop() {
            super.onStop();
            mForceGoMain = true;
        }
    
        /**
         * 加载开屏广告
         */
        public void loadSplashAd() {
            AdSlot adSlot = null;
            float splashWidthDp = UIUtils.getScreenWidthDp(this);
            int splashWidthPx = UIUtils.getScreenWidthInPx(this);
            int screenHeightPx = UIUtils.getScreenHeight(this);
            float screenHeightDp = UIUtils.px2dip(this, screenHeightPx);
            float splashHeightDp;
            int splashHeightPx;
            if (mIsHalfSize) {
                // 开屏高度 = 屏幕高度 - 下方预留的高度,demo中是预留了屏幕高度的1/5,因此开屏高度传入 屏幕高度*4/5
                splashHeightDp = screenHeightDp * 4 / 5.f;
                splashHeightPx = (int) (screenHeightPx * 4 / 5.f);
            } else {
                splashHeightDp = screenHeightDp;
                splashHeightPx = screenHeightPx;
            }
            if (mIsExpress) {
                //个性化模板广告需要传入期望广告view的宽、高,单位dp,请传入实际需要的大小,
                //比如:广告下方拼接logo、适配刘海屏等,需要考虑实际广告大小
    //            float expressViewWidth = UIUtils.getScreenWidthDp(this);
    //            float expressViewHeight = UIUtils.getHeight(this);
                adSlot = new AdSlot.Builder()
                        .setCodeId(mCodeId)
                        .setSupportDeepLink(true)
                        .setImageAcceptedSize(splashWidthPx, splashHeightPx)
                        //模板广告需要设置期望个性化模板广告的大小,单位dp,代码位是否属于个性化模板广告,请在穿山甲平台查看
                        .setExpressViewAcceptedSize(splashWidthDp, splashHeightDp)
                        .build();
            } else {
                adSlot = new AdSlot.Builder()
                        .setCodeId(mCodeId)
                        .setSupportDeepLink(true)
                        .setImageAcceptedSize(splashWidthPx, splashHeightPx)
                        .build();
            }
            //step4:请求广告,调用开屏广告异步请求接口,对请求回调的广告作渲染处理
            mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
                @Override
                public void onError(int code, String message) {
                    Log.d(TAG, "loadSplashAd OnError" + message + " code:" + code);
                    showToast(message);
                    goToMainActivity();
                }
    
                @Override
                public void onTimeout() {
                    Log.d(TAG, "loadSplashAd onTimeout");
                    showToast("开屏广告加载超时");
                    goToMainActivity();
                }
    
                @Override
                public void onSplashAdLoad(com.bytedance.sdk.openadsdk.TTSplashAd ad) {
                    Log.d(TAG, "loadSplashAd success");
                    Log.d(TAG, "开屏广告请求成功");
                    if (ad == null) {
                        return;
                    }
                    //获取SplashView
                    View view = ad.getSplashView();
                    if (view != null && mSplashContainer != null && !SplashActivity.this.isFinishing()) {
                        mSplashContainer.removeAllViews();
                        //把SplashView 添加到ViewGroup中,注意开屏广告view:width >=70%屏幕宽;height >=50%屏幕高
                        mSplashContainer.addView(view);
                        //设置不开启开屏广告倒计时功能以及不显示跳过按钮,如果这么设置,您需要自定义倒计时逻辑
                        //ad.setNotAllowSdkCountdown();
                    } else {
                        goToMainActivity();
                    }
                    //设置SplashView的交互监听器
                    ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                        @Override
                        public void onAdClicked(View view, int type) {
                            Log.d(TAG, "onAdClicked");
                            showToast("开屏广告点击");
                        }
    
                        @Override
                        public void onAdShow(View view, int type) {
                            Log.d(TAG, "onAdShow");
                            showToast("开屏广告展示");
                        }
    
                        @Override
                        public void onAdSkip() {
                            Log.d(TAG, "onAdSkip");
                            showToast("开屏广告跳过");
                            goToMainActivity();
                        }
    
                        @Override
                        public void onAdTimeOver() {
                            Log.d(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);
        }
    
        /**
         * 跳转到主页面
         */
        private void goToMainActivity() {
            Intent intent = new Intent(SplashActivity.this, MainActivity.class);
            startActivity(intent);
            mSplashContainer.removeAllViews();  //移除所有视图
            this.finish();
        }
    
        private void showToast(String msg) {
            TToast.show(this, msg);
        }
    
        /**
         * 隐藏虚拟按键,并且全屏
         */
        protected void hideBottomUIMenu() {
            //隐藏虚拟按键,并且全屏
            if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
                View v = this.getWindow().getDecorView();
                v.setSystemUiVisibility(View.GONE);
            } else if (Build.VERSION.SDK_INT >= 19) {
                //for new api versions.
                View decorView = getWindow().getDecorView();
                int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
                decorView.setSystemUiVisibility(uiOptions);
            }
        }
    }
    
    • 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

    activity_splash.xml布局文件内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/splash_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        tools:context=".SplashActivity">
    
    </FrameLayout>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    AndroidMainfest.xmlSplashActivity界面当做启动界面,移动.MainActivity下的启动过滤器到.SplashActivity下配置即可。

    <activity
        android:name=".MainActivity"
        android:exported="true" />
    <activity
        android:name=".SplashActivity"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    开屏广告配置完毕。运行项目测试广告展示情况即可!出现问题根据控制台和弹窗提示排查解决。

    穿山甲之之插屏广告

    首先创建一个类InsertScreen.java,代码如下:

    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.Context;
    import android.util.Log;
    import android.widget.Toast;
    import com.bytedance.sdk.openadsdk.AdSlot;
    import com.bytedance.sdk.openadsdk.TTAdConstant;
    import com.bytedance.sdk.openadsdk.TTAdLoadType;
    import com.bytedance.sdk.openadsdk.TTAdManager;
    import com.bytedance.sdk.openadsdk.TTAdNative;
    import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
    import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
    import com.yrj.csjdemo.Utils.TToast;
    
    //新插屏广告
    public class InsertScreen {
        private final String TAG = "InsertScreen";
        @SuppressLint("StaticFieldLeak")
        private static InsertScreen _Instance;
    
        public static InsertScreen Inst() {
            if (_Instance == null) {
                _Instance = new InsertScreen();
            }
            return _Instance;
        }
    
        private TTAdNative mTTAdNative;
        private TTFullScreenVideoAd mttFullVideoAd;
        private boolean mIsLoaded = false; //视频是否加载完成
        private Context mContext;
    
        public void Init(Context context, String codeId, int orientation) {
            mContext = context;
            TTAdManager ttAdManager = TTAdManagerHolder.get();
            //step2:(可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
    //        TTAdManagerHolder.get().requestPermissionIfNecessary(mContext);
            //step3:创建TTAdNative对象,用于调用广告请求接口
            mTTAdNative = ttAdManager.createAdNative(mContext);
            loadAd(codeId, orientation);
        }
    
        private boolean mHasShowDownloadActive = false;
    
        private void loadAd(String codeId, int orientation) {
            //step4:创建广告请求参数AdSlot,具体参数含义参考文档
            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(codeId)
                    .setSupportDeepLink(true)
                    .setAdLoadType(TTAdLoadType.LOAD)//推荐使用,用于标注此次的广告请求用途为预加载(当做缓存)还是实时加载,方便后续为开发者优化相关策略
                    .setOrientation(orientation)
                    .build();
            //step5:请求广告
            mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
                @Override
                public void onError(int code, String message) {
                    Log.e(TAG, "Callback --> onError: " + code + ", " + String.valueOf(message));
                    TToast.show(mContext, message);
                }
    
                @Override
                public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {
                    Log.e(TAG, "Callback --> onFullScreenVideoAdLoad");
    
                    TToast.show(mContext, "FullVideoAd loaded  广告类型:" + getAdType(ad.getFullVideoAdType()));
                    mttFullVideoAd = ad;
                    mIsLoaded = false;
    
                    mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {
    
                        @Override
                        public void onAdShow() {
                            Log.d(TAG, "Callback --> FullVideoAd show");
                            TToast.show(mContext, "FullVideoAd show");
                        }
    
                        @Override
                        public void onAdVideoBarClick() {
                            Log.d(TAG, "Callback --> FullVideoAd bar click");
                            TToast.show(mContext, "FullVideoAd bar click");
                        }
    
                        @Override
                        public void onAdClose() {
                            Log.d(TAG, "Callback --> FullVideoAd close");
                            TToast.show(mContext, "FullVideoAd close");
                        }
    
                        @Override
                        public void onVideoComplete() {
                            Log.d(TAG, "Callback --> FullVideoAd complete");
                            TToast.show(mContext, "FullVideoAd complete");
                        }
    
                        @Override
                        public void onSkippedVideo() {
                            Log.d(TAG, "Callback --> FullVideoAd skipped");
                            TToast.show(mContext, "FullVideoAd skipped");
    
                        }
    
                    });
    
                    ad.setDownloadListener(new TTAppDownloadListener() {
                        @Override
                        public void onIdle() {
                            mHasShowDownloadActive = false;
                        }
    
                        @Override
                        public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                            Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
    
                            if (!mHasShowDownloadActive) {
                                mHasShowDownloadActive = true;
                                TToast.show(mContext, "下载中,点击下载区域暂停", Toast.LENGTH_LONG);
                            }
                        }
    
                        @Override
                        public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                            Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
                            TToast.show(mContext, "下载暂停,点击下载区域继续", Toast.LENGTH_LONG);
                        }
    
                        @Override
                        public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                            Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
                            TToast.show(mContext, "下载失败,点击下载区域重新下载", Toast.LENGTH_LONG);
                        }
    
                        @Override
                        public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                            Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);
                            TToast.show(mContext, "下载完成,点击下载区域重新下载", Toast.LENGTH_LONG);
                        }
    
                        @Override
                        public void onInstalled(String fileName, String appName) {
                            Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);
                            TToast.show(mContext, "安装完成,点击下载区域打开", Toast.LENGTH_LONG);
                        }
                    });
                }
    
                @Override
                public void onFullScreenVideoCached() {
                    Log.d(TAG, " onFullScreenVideoCached");
                    TToast.show(mContext, "onFullScreenVideoCached");
                }
    
                @Override
                public void onFullScreenVideoCached(TTFullScreenVideoAd ad) {
                    Log.e(TAG, "Callback --> onFullScreenVideoCached");
                    mIsLoaded = true;
                    TToast.show(mContext, "FullVideoAd video cached");
                    ad.showFullScreenVideoAd((Activity) mContext, TTAdConstant.RitScenes.GAME_GIFT_BONUS, null);
                }
            });
        }
    
        private String getAdType(int type) {
            switch (type) {
                case TTAdConstant.AD_TYPE_COMMON_VIDEO:
                    return "普通全屏视频,type=" + type;
                case TTAdConstant.AD_TYPE_PLAYABLE_VIDEO:
                    return "Playable全屏视频,type=" + type;
                case TTAdConstant.AD_TYPE_PLAYABLE:
                    return "纯Playable,type=" + type;
                case TTAdConstant.AD_TYPE_LIVE:
                    return "直播流,type=" + type;
            }
            return "未知类型+type=" + type;
        }
    }
    
    • 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

    然后在主界面MainActivity.java或其他界面的Oncreate()方法下做调用,代码如下:

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.FrameLayout;
    import com.bytedance.sdk.openadsdk.TTAdConstant;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //获取到声明中的权限,提高广告变现效率。记得禁止插屏和Banner广告的权限申请否则会重复。
            TTAdManagerHolder.get().requestPermissionIfNecessary(MyApplication.getContext());
            //新插屏广告                    插屏广告代码位id
            InsertScreen.Inst().Init(this, "这里写插屏广告代码位id", TTAdConstant.HORIZONTAL);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    插屏广告配置完毕。运行项目测试广告展示情况即可!

    穿山甲之Banner广告

    首先创建一个类Banner.java,代码如下:

    import android.app.Activity;
    import android.content.Context;
    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.TTAdConstant;
    import com.bytedance.sdk.openadsdk.TTAdDislike;
    import com.bytedance.sdk.openadsdk.TTAdLoadType;
    import com.bytedance.sdk.openadsdk.TTAdNative;
    import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
    import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
    import com.yrj.csjdemo.Utils.TToast;
    import com.yrj.csjdemo.Utils.UIUtils;
    
    import java.util.List;
    
    public class Banner {
        private static Banner _Insstance;
        public static Banner Inst() {
            if (_Insstance == null) {
                _Insstance = new Banner();
            }
            return _Insstance;
        }
        private TTAdNative mTTAdNative;
        private FrameLayout mExpressContainer;
        private Context mContext;
        private final long startTime = 0;
        private boolean mHasShowDownloadActive = false;
    
        public void Init(Context context, FrameLayout frameLayout, String codeId) {
            mContext = context;
            mExpressContainer = frameLayout;
            //step2:创建TTAdNative对象
            mTTAdNative = TTAdManagerHolder.get().createAdNative(context);
            //step3:可选,强烈建议在合适的时机调用):申请部分权限,如read_phone_state,防止获取不了imei时候,下载类广告没有填充的问题。
    //        TTAdManagerHolder.get().requestPermissionIfNecessary(context);
            loadBannerAd(codeId);
        }
    
        private void loadBannerAd(String codeId) {
            //step4:创建广告请求参数AdSlot,注意其中的setNativeAdtype方法,具体参数含义参考文档
            int width = UIUtils.getScreenWidthInPx(mContext);
            int height = UIUtils.getScreenHeight(mContext);
            AdSlot adSlot = new AdSlot.Builder()
                    .setCodeId(codeId) //广告位id
                    .setSupportDeepLink(true)
                    .setAdCount(1) //请求广告数量为1 建议调整为1~3
                    .setExpressViewAcceptedSize(width, 50) //期望模板广告view的size,单位dp
                    .setAdLoadType(TTAdLoadType.LOAD)//推荐使用,用于标注此次的广告请求用途为预加载(当做缓存)还是实时加载,方便后续为开发者优化相关策略
                    .build();
            //step5:请求广告,对请求回调的广告作渲染处理
            mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
                //请求失败回调
                @Override
                public void onError(int code, String message) {
                    TToast.show(mContext, "load error : " + code + ", " + message);
                    mExpressContainer.removeAllViews();
                }
    
                //请求成功回调
                @Override
                public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                    if (ads.get(0) == null) {
                        return;
                    }
    
                    final TTNativeExpressAd ad = ads.get(0);
                    ad.setSlideIntervalTime(30 * 1000);
                    bindAdListener(ad);
                    ad.render();
                    TToast.show(mContext, "load success!");
                }
            });
    
        }
    
        private void bindAdListener(TTNativeExpressAd ad) {
    
            ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
                @Override
                public void onAdClicked(View view, int type) {
                    TToast.show(mContext, "广告被点击");
                }
    
                @Override
                public void onAdShow(View view, int type) {
                    TToast.show(mContext, "广告展示");
                }
    
                @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, "渲染成功");
                    mExpressContainer.removeAllViews();
                    mExpressContainer.addView(view);
                }
            });
            //dislike设置
            bindDislike(ad, false);
    
            if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
                return;
            }
            ad.setDownloadListener(new TTAppDownloadListener() {
                @Override
                public void onIdle() {
                    TToast.show(mContext, "点击开始下载", Toast.LENGTH_LONG);
                }
    
                @Override
                public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                    if (!mHasShowDownloadActive) {
                        mHasShowDownloadActive = true;
                        TToast.show(mContext, "下载中,点击暂停", Toast.LENGTH_LONG);
                    }
                }
    
                @Override
                public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                    TToast.show(mContext, "下载暂停,点击继续", Toast.LENGTH_LONG);
                }
    
                @Override
                public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                    TToast.show(mContext, "下载失败,点击重新下载", Toast.LENGTH_LONG);
                }
    
                @Override
                public void onInstalled(String fileName, String appName) {
                    TToast.show(mContext, "安装完成,点击图片打开", Toast.LENGTH_LONG);
                }
    
                @Override
                public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                    TToast.show(mContext, "点击安装", Toast.LENGTH_LONG);
                }
            });
        }
    
        /**
         * 设置广告的不喜欢, 注意:强烈建议设置该逻辑,如果不设置dislike处理逻辑,则模板广告中的 dislike区域不响应dislike事件。
         *
         * @param ad
         * @param customStyle 是否自定义样式,true:样式自定义
         */
    
        private void bindDislike(TTNativeExpressAd ad, boolean customStyle) {
            //使用默认模板中默认dislike弹出样式
            ad.setDislikeCallback((Activity) mContext, new TTAdDislike.DislikeInteractionCallback() {
                @Override
                public void onShow() {
                    TToast.show(mContext, "bindDislike setDislikeCallback onShow");
                }
    
                @Override
                public void onSelected(int position, String value, boolean enforce) {
                    TToast.show(mContext, "点击 " + value);
                    mExpressContainer.removeAllViews();
                    //用户选择不喜欢原因后,移除广告展示
                    if (enforce) {
                        TToast.show(mContext, "模版Banner 穿山甲sdk强制将view关闭了");
                    }
                }
    
                @Override
                public void onCancel() {
                    TToast.show(mContext, "点击取消 ");
                }
            });
        }
    }
    
    • 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

    然后在主界面MainActivity.java或其他界面的布局xml文件中的合适位置,设置广告显示布局,代码如下:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="2dp"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:id="@+id/linearLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            tools:ignore="MissingConstraints">
    
            <FrameLayout
                android:id="@+id/banner_container"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                tools:ignore="MissingConstraints" />
    
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:text="这里是一行示例布局展示文字"
                android:textSize="18sp" />
        </LinearLayout>
    </androidx.constraintlayout.widget.ConstraintLayout>
    
    • 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

    然后在主界面MainActivity.java或其他界面的Oncreate()方法下做调用,代码如下:

    import androidx.appcompat.app.AppCompatActivity;
    import android.os.Bundle;
    import android.widget.FrameLayout;
    import com.bytedance.sdk.openadsdk.TTAdConstant;
    
    public class MainActivity extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            //获取到声明中的权限,提高广告变现效率。记得禁止插屏和Banner广告的权限申请否则会重复。
            TTAdManagerHolder.get().requestPermissionIfNecessary(MyApplication.getContext());
            //新插屏广告                              插屏广告代码位id
            InsertScreen.Inst().Init(this, "这里写插屏广告代码位id", TTAdConstant.HORIZONTAL);
            //banner广告   banner广告代码位id
            FrameLayout mExpressBannerContainer = findViewById(R.id.banner_container);
            Banner.Inst().Init(this, mExpressBannerContainer, "这里写banner广告代码位id");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    Banner广告配置完毕。运行项目测试广告展示情况即可!

    穿山甲之信息流广告

    暂时不写

    穿山甲之激励视频广告

    暂时不写

    参考链接:
    参考作者主页
    Android 接入穿山甲广告
    Android 接入穿山甲SDK之开屏广告
    Android 接入穿山甲SDK之插屏广告
    Android 接入穿山甲SDK之信息流广告
    Android 接入穿山甲SDK之Banner广告
    Android 接入穿山甲SDK之激励视频广告
    等等,还有一些其它教程和解错文章,感谢!!

  • 相关阅读:
    使用g2opy 做一个简单的二维回环优化 Slam(附python代码)
    Eclipse 主网即将上线迎空投预期,Zepoch 节点或成受益者?
    跨境电商卖家培训影响视频推荐的原因
    Flink Icerberg 离线输仓-维度建模过程(二)
    [计算机网络] 三次握手四次挥手
    vue中的h函数与JSX语法
    第七章 网络安全 | 计算机网络(谢希仁 第八版)
    sourcetree 配置 gitlab ssh及公钥私钥设置
    医学图像分类 神经网络,神经网络和图神经网络
    WIN10专业版64位21H2正式版19044.1826
  • 原文地址:https://blog.csdn.net/yi_rui_jie/article/details/125535082