java - Android AdMob原生广告便捷类

在包含本地广告的我的应用程序工作期间,创建了方便类,以与积压的“集群方式”一起使用。

import android.content.Context;
import android.os.AsyncTask;
import android.util.SparseArray;

import com.google.android.ads.nativetemplates.TemplateView;
import com.google.android.gms.ads.AdLoader;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.formats.UnifiedNativeAd;

import java.util.Stack;
import java.util.concurrent.atomic.AtomicBoolean;

public class AdService {
    class BacklogItem {
        TemplateView holder;
        int hash;
    }
    private AtomicBoolean isLoading  = new AtomicBoolean(false);
    private SparseArray<UnifiedNativeAd> adsMapping = new SparseArray<>();
    private Stack<UnifiedNativeAd> adsCache = new Stack<UnifiedNativeAd>();
    private Stack<BacklogItem> adsBacklog = new Stack<BacklogItem>();

    private final int loadSize = 3;
    private final int minSize = 2;

    private final AdRequest adRequest = new AdRequest
            .Builder()
            .build();

    private AdLoader adLoader = null;
    public static AdService instance = new AdService();

    private void load()
    {
        boolean wasNewValueSet = isLoading.compareAndSet(
                false, true);
        if (!wasNewValueSet) {
            return;
        }
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                adLoader.loadAds(adRequest, loadSize);
                isLoading.set(false);
            }
        });
    }

    public void init(Context context)
    {
        if (adLoader == null) {
            adLoader = new AdLoader.Builder(context, context.getString(R.string.native_ad_unit_id))
                    .forUnifiedNativeAd(new UnifiedNativeAd.OnUnifiedNativeAdLoadedListener() {
                        @Override
                        public void onUnifiedNativeAdLoaded(UnifiedNativeAd unifiedNativeAd) {
                            if (adsBacklog.isEmpty()) {
                                adsCache.push(unifiedNativeAd);
                            } else {
                                BacklogItem item = adsBacklog.pop();
                                _setAd(unifiedNativeAd, item.holder, item.hash);
                            }
                        }
                    })
                    .build();
        }
        load();
    }

    private void _setAd(UnifiedNativeAd unifiedNativeAd, TemplateView holder, int hash)
    {
        holder.setNativeAd(unifiedNativeAd);
        adsMapping.put(hash, unifiedNativeAd);
    }

    public void setAd(TemplateView holder, int hash)
    {
        if (adLoader == null) {
            init(holder.getContext());
        }
        if (adsCache.size() < minSize) {
            load();
        }
        UnifiedNativeAd unifiedNativeAd = adsMapping.get(hash);
        if (unifiedNativeAd != null) {
            holder.setNativeAd(unifiedNativeAd);
        } else {
            if (adsCache.isEmpty()) {
                // If no ads in cache wait for
                // it and put task in backlog
                BacklogItem item = new BacklogItem();
                item.hash = hash;
                item.holder = holder;
                adsBacklog.push(item);
            } else {
                // otherwise get ad from cache
                unifiedNativeAd = adsCache.pop();
                _setAd(unifiedNativeAd, holder, hash);
            }
        }
    }
}

MainActivity onCreate

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        AdService.instance.init(this);
    ....
    }

onBindViewHolder

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position)
    {
    .......
                AdService.instance.setAd(myView, myHash);
    .......
    }

人们对此有何建议?

转载请注明来自askonline.tech,本文标题:java - Android AdMob原生广告便捷类


 Top