Android AdMob Native ads convenience class

懵懂的女人 提交于 2020-03-05 03:22:07

问题


During work on my app containing native ads created convenience class to work with them "cluster way" with backlog.

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);
            }
        }
    }
}

in MainActivity onCreate

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

in onBindViewHolder

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

What people can recommend about it? This code been used in anonymous chat app https://play.google.com/store/apps/details?id=com.lonje

来源:https://stackoverflow.com/questions/59114495/android-admob-native-ads-convenience-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!