问题
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