admin
2022-05-07 4c7cde7ae5ed57335405459e47de4bbd2726c4ba
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
package com.demo.library_ad;
 
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
 
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.demo.lib.common.util.common.DimenUtils;
import com.qq.e.ads.splash.SplashAD;
import com.qq.e.ads.splash.SplashADListener;
import com.qq.e.comm.util.AdError;
 
public class SplashAdUtil {
 
    /**
     * 加载开屏广告
     *
     * @param adType
     * @param pid
     * @param context
     * @param vg_ad
     * @param splashAdListener
     */
    public static void loadAD(AdUtil.AD_TYPE adType, String pid, Activity context, ViewGroup vg_ad, final SplashAdListener splashAdListener) {
        if (adType == null) {
            splashAdListener.close();
            return;
        }
        if (AdUtil.AD_TYPE.csj == adType) {
            loadCSJ(context, pid, vg_ad, splashAdListener);
        } else if (AdUtil.AD_TYPE.gdt == adType) {
            loadGDT(context, pid, vg_ad, splashAdListener);
        } else {
            splashAdListener.close();
        }
    }
 
    private static void loadGDT(Activity context, String pid, ViewGroup vg_ad, final SplashAdListener splashAdListener) {
        new SplashAD(context, pid, new SplashADListener() {
            @Override
            public void onADDismissed() {
                splashAdListener.close();
            }
 
            @Override
            public void onNoAD(AdError adError) {
                splashAdListener.noAd();
            }
 
            @Override
            public void onADPresent() {
            }
 
            @Override
            public void onADClicked() {
                System.out.println();
            }
 
            @Override
            public void onADTick(final long l) {
 
            }
 
            @Override
            public void onADExposure() {
                System.out.println();
            }
 
            @Override
            public void onADLoaded(long l) {
            }
            //请求超时时间为4s
        }, 4000).fetchAndShowIn(vg_ad);
    }
 
 
    /**
     * 加载穿山甲广告
     *
     * @param context
     * @param vg_ad
     * @param splashAdListener
     */
    /**
     * @param context
     * @param code             代码位
     * @param vg_ad
     * @param splashAdListener
     */
    private static void loadCSJ(Activity context, String code, final ViewGroup vg_ad, final SplashAdListener splashAdListener) {
        TTAdManager ttAdManager = null;
        try {
            ttAdManager = TTAdManagerHolder.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (ttAdManager == null) {
            if (splashAdListener != null)
                splashAdListener.noAd();
            return;
        }
        TTAdNative mTTAdNative = ttAdManager.createAdNative(context.getApplicationContext());
        //穿山甲广告
        int width = DimenUtils.getScreenWidth(context);
        int height = DimenUtils.getScreenHeight(context);
        if (width == 0)
            width = 720;
        if (height == 0)
            height = 1080;
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(code)
                .setSupportDeepLink(true)
                .setImageAcceptedSize(width, height)
                .build();
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
 
            @Override
            public void onError(int i, String s) {
                splashAdListener.noAd();
            }
 
            @Override
            public void onTimeout() {
                splashAdListener.noAd();
            }
 
            @Override
            public void onSplashAdLoad(TTSplashAd ad) {
                if (ad == null) {
                    splashAdListener.noAd();
                    return;
                }
                View view = ad.getSplashView();
                vg_ad.removeAllViews();
                //把SplashView 添加到ViewGroup中
                vg_ad.addView(view);
                //设置SplashView的交互监听器
                ad.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int type) {
                    }
 
                    @Override
                    public void onAdShow(View view, int type) {
                    }
 
                    @Override
                    public void onAdSkip() {
                        splashAdListener.close();
                    }
 
                    @Override
                    public void onAdTimeOver() {
                        splashAdListener.close();
                    }
                });
            }
        }, 5000);
 
 
    }
 
    public interface SplashAdListener {
        void close();
 
        void noAd();
    }
 
}