Android 华为广告SDK接入Demo - 13.4.40.302版本

  • p3_495854
    了解作者
  • 833KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-17 12:42
    上传日期
华为广告SDK(4月9号V13.4.40.302)版本,里面包含 开屏,激励,插屏,banner,原生等广告,已经接好,封装,只需要调用加载和展示的接口就可以使用。
HMSAdController-13.4.40.302.zip
内容介绍
package com.hmsadcontroller.Ads; import android.app.Activity; import android.graphics.Color; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.TextView; import com.huawei.hms.ads.AdListener; import com.huawei.hms.ads.AdParam; import com.huawei.hms.ads.nativead.DetailedCreativeType; import com.huawei.hms.ads.nativead.NativeAd; import com.huawei.hms.ads.nativead.NativeAdLoader; import com.huawei.hms.ads.nativead.NativeView; import java.util.ArrayList; import java.util.List; /** * 华为原生视频广告 * @author JZ * @time 23/04/2021 */ public class HMSNativeAd { private static HMSNativeAd hmsNativeAd = null; private static final String TAG = "HMS-NativeAd"; private NativeAdLoader nativeAdLoader; private NativeView nativeView; private FrameLayout frameLayout; private TextView close; private static boolean isFirst = true; public static HMSNativeAd getInstance(){ if (null == hmsNativeAd){ synchronized (HMSNativeAd.class){ if (null == hmsNativeAd){ hmsNativeAd = new HMSNativeAd(); } } } return hmsNativeAd; } public void loadHMSNativeAd(Activity activity, String id){ NativeAdLoader.Builder builder = new NativeAdLoader.Builder(activity, id); builder.setNativeAdLoadedListener(new NativeAd.NativeAdLoadedListener() { @Override public void onNativeAdLoaded(NativeAd nativeAd) { Log.e(TAG,"onNativeAdLoaded-----"); activity.runOnUiThread(new Runnable() { @Override public void run() { if (isFirst == true){ getId(activity); } // 注册和填充原生广告素材视图 initNativeAdView(activity, nativeAd, id); } }); } }).setAdListener(new AdListener() { @Override public void onAdFailed(int errorCode) { // 广告获取失败时调用 Log.e(TAG,"onAdFailed-----errorCode = " + errorCode); } }); nativeAdLoader = builder.build(); } /** * 展示 */ public void showHMSNativeAd(){ List<Integer> detailedCreativeTypeList = new ArrayList<>(); // 添加指定的创意类型 detailedCreativeTypeList.add(DetailedCreativeType.VIDEO); AdParam.Builder adRequestBuilder = new AdParam.Builder(); adRequestBuilder.setDetailedCreativeTypeList(detailedCreativeTypeList); nativeAdLoader.loadAd(adRequestBuilder.build()); } /** * 设置广告视图信息 * @param activity * @param nativeAd */ private void initNativeAdView(Activity activity, NativeAd nativeAd, String id) { // 注册和填充标题素材视图 ((TextView) nativeView.getTitleView()).setText(nativeAd.getTitle()); //注册和填充广告来源素材视图 ((TextView) nativeView.getAdSourceView()).setText(nativeAd.getAdSource()); //注册和填充广告点击行为按钮素材视图 ((TextView) nativeView.getCallToActionView()).setText(nativeAd.getCallToAction()); // 注册和填充多媒体素材视图 nativeView.getMediaView().setMediaContent(nativeAd.getMediaContent()); close.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { frameLayout.removeView(nativeView); nativeAd.destroy(); loadHMSNativeAd(activity, id); } }); // 注册和填充其他素材视图 // 注册原生广告对象 nativeView.setNativeAd(nativeAd); frameLayout.addView(nativeView); } /** * 初始化视图 * @param activity */ private void getId(Activity activity){ String pkName = activity.getPackageName(); int layoutId = activity.getResources().getIdentifier("native_view","layout", pkName); int ad_media = activity.getResources().getIdentifier("ad_media","id", pkName); int ad_title = activity.getResources().getIdentifier("ad_title","id", pkName); int ad_source = activity.getResources().getIdentifier("ad_source","id", pkName); int ad_action = activity.getResources().getIdentifier("ad_action","id", pkName); int ad_close = activity.getResources().getIdentifier("ad_close","id", pkName); // 获取NativeView视图 nativeView = (NativeView) activity.getLayoutInflater().inflate(layoutId, null); nativeView.setMediaView(nativeView.findViewById(ad_media)); nativeView.setTitleView(nativeView.findViewById(ad_title)); nativeView.setAdSourceView(nativeView.findViewById(ad_source)); nativeView.setCallToActionView(nativeView.findViewById(ad_action)); close = nativeView.findViewById(ad_close); FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); frameLayout = new FrameLayout(activity); layoutParams.gravity = Gravity.CENTER; frameLayout.setBackgroundColor(Color.WHITE); activity.addContentView(frameLayout,layoutParams); isFirst = false; } }
评论
    相关推荐