方法 | 参数 | 返回值 | 说明 | 是否必须实现 |
---|---|---|---|---|
show | Activity activity:Activity ViewGroup container:Container of ad | void | Show splash ad | Yes |
CustomSplashAdapter提供了广告加载超时时间的成员变量,即开发者通过ATSplashAd构造方法传入的fetchAdTimeout变量,该变量可用于设置广告平台的超时时间。
使用CustomSplashAdapter的CustomSplashEventListener成员变量实现广告事件的回调
回调方法 | 参数说明 | 说明 |
---|---|---|
onSplashAdClicked | - | 广告被点击时执行的回调给开发者 |
onSplashAdShow | - | 广告展示时执行的回调给开发者 |
onSplashAdDismiss | - | 广告被关闭时执行的回调给开发者 |
注意:使用成员变量CustomSplashEventListener时候需要做判空处理,因为聚合SDK内部有可能会回收该Listener对象。
public class PangleSplashAdapter extends CustomSplashAdapter implements TTSplashAd.AdInteractionListener {
private final String TAG = getClass().getSimpleName();
String appId = "";
String slotId = "";
String personalizedTemplate = "";
TTSplashAd splashAd;
@Override
public void loadCustomNetworkAd(final Context context, Map<String, Object> serverExtra, final Map<String, Object> localExtra) {
if (serverExtra.containsKey("app_id") && serverExtra.containsKey("slot_id")) {
appId = (String) serverExtra.get("app_id");
slotId = (String) serverExtra.get("slot_id");
} else {
if (mLoadListener != null) {
mLoadListener.onAdLoadError("", "app_id or slot_id is empty!");
}
return;
}
personalizedTemplate = "0";
if (serverExtra.containsKey("personalized_template")) {
personalizedTemplate = (String) serverExtra.get("personalized_template");
}
PangleInitManager.getInstance().initSDK(context, serverExtra, true, new PangleInitManager.InitCallback() {
@Override
public void onFinish() {
startLoad(context, localExtra);
}
});
}
private void startLoad(Context context, Map<String, Object> localExtra) {
TTAdManager ttAdManager = TTAdSdk.getAdManager();
final TTAdNative mTTAdNative = ttAdManager.createAdNative(context);//baseContext is recommended for activity
final AdSlot.Builder adSlotBuilder = new AdSlot.Builder().setCodeId(slotId);
int width = 0;
int height = 0;
try {
if (localExtra.containsKey(ATAdConst.KEY.AD_WIDTH)) {
width = Integer.parseInt(localExtra.get(ATAdConst.KEY.AD_WIDTH).toString());
}
} catch (Throwable e) {
e.printStackTrace();
}
try {
if (localExtra.containsKey(ATAdConst.KEY.AD_HEIGHT)) {
height = Integer.parseInt(localExtra.get(ATAdConst.KEY.AD_HEIGHT).toString());
}
} catch (Throwable e) {
e.printStackTrace();
}
adSlotBuilder.setImageAcceptedSize(width, height); //Must be set
if (TextUtils.equals("1", personalizedTemplate)) {// Native Express
adSlotBuilder.setExpressViewAcceptedSize(width, height);
}
postOnMainThread(new Runnable() {
@Override
public void run() {
AdSlot adSlot = adSlotBuilder.build();
mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
@Override
public void onError(int i, String s) {
if (mLoadListener != null) {
mLoadListener.onAdLoadError(i + "", s);
}
}
@Override
public void onTimeout() {
if (mLoadListener != null) {
mLoadListener.onAdLoadError("", "onTimeout");
}
}
@Override
public void onSplashAdLoad(TTSplashAd ttSplashAd) {
splashAd = ttSplashAd;
if (mLoadListener != null) {
mLoadListener.onAdCacheLoaded();
}
}
}, mFetchAdTimeout);
}
});
}
@Override
public boolean isAdReady() {
return splashAd != null;
}
@Override
public void show(Activity activity, ViewGroup container) {
if (splashAd != null) {
splashAd.setSplashInteractionListener(PangleSplashAdapter.this);
View splashView = splashAd.getSplashView();
if (splashView != null) {
container.addView(splashView);
}
}
}
@Override
public String getNetworkName() {
return PangleInitManager.getInstance().getNetworkName();
}
@Override
public void destory() {
}
@Override
public String getNetworkPlacementId() {
return slotId;
}
@Override
public String getNetworkSDKVersion() {
return PangleInitManager.getInstance().getNetworkVersion();
}
@Override
public void onAdClicked(View view, int i) {
if (mImpressionListener != null) {
mImpressionListener.onSplashAdClicked();
}
}
@Override
public void onAdShow(View view, int i) {
if (mImpressionListener != null) {
mImpressionListener.onSplashAdShow();
}
}
@Override
public void onAdSkip() {
if (mImpressionListener != null) {
mImpressionListener.onSplashAdDismiss();
}
}
@Override
public void onAdTimeOver() {
if (mImpressionListener != null) {
mImpressionListener.onSplashAdDismiss();
}
}
}