如何在打开活动时无延迟加载插页广告

huangapple go评论61阅读模式
英文:

how to load interstitial ad without delay when open the Activity

问题

我在onCreate方法中加载插页广告,然后在用户点击按钮时显示它,但是在打开活动时会有延迟,大约需要2到3秒才能打开活动。我尝试在后台加载广告,但不起作用,错误显示为必须在主UI线程上调用。

以下是AdsManger类:

public class AdsManger {
    Context context;
    InterstitialAd mInterstitialAd;
    AdRequest adRequest;
    int adLoadRetry=0;

    public AdsManger(Context context) {
        this.context = context;
        triggerAd();
    }

    public void triggerAd(){
        MobileAds.initialize(context, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {
            }
        });

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

        InterstitialAd.load(context, "UnitID", adRequest, new InterstitialAdLoadCallback() {
            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                super.onAdFailedToLoad(loadAdError);
                mInterstitialAd = null;
                Toast.makeText(context, "广告加载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                super.onAdLoaded(interstitialAd);
                mInterstitialAd = interstitialAd;
            }
        });

        if(mInterstitialAd == null){
            if(adLoadRetry < 3){
                adLoadRetry++;
                triggerAd();
            }
        }
    }

    public void ShowAd(){
        if (mInterstitialAd != null){
            mInterstitialAd.show((Activity) context);
        }
    }
}

以下是我的onCreate方法:

@SuppressLint("MissingInflatedId")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_emailwriter);
       
    AdsManger adsManger = new AdsManger(this);

    button = findViewById(R.id.Button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            adsManger.showAd();
        }
    });
}
英文:

im loading the interstitial ad inside onCreate method then i show it when the user click the button but there's a delay when opening the activity it takes about 2 to 3 seconds to open the activity i tried to load the ad in background but it's not working the error showing is Must be called on the main UI thread

here's the adsmanger class

public class AdsManger {
Context context;
InterstitialAd mInterstitialAd;
AdRequest adRequest;
int adLoadRetry=0;
public AdsManger(Context context) {
this.context = context;
triggerAd();
}
public void triggerAd(){
MobileAds.initialize(context, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(@NonNull InitializationStatus initializationStatus) {
}});
adRequest = new AdRequest.Builder().build();
InterstitialAd.load(context, &quot;UnitID&quot;, adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
mInterstitialAd =null;
Toast.makeText(context, &quot;ad loading failed&quot;, Toast.LENGTH_SHORT).show();
}
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
super.onAdLoaded(interstitialAd);
mInterstitialAd=interstitialAd;
}
});
if(mInterstitialAd==null){
if(adLoadRetry&lt;3){
adLoadRetry++;
triggerAd();
}
}}
public void ShowAd(){
if (mInterstitialAd != null){
mInterstitialAd.show((Activity) context);
}
}}

and here's my on create method

@SuppressLint(&quot;MissingInflatedId&quot;)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_emailwriter);
AdsManger adsManger=new AdsManger(this);
button=findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
adsManger.showAd();});

答案1

得分: 0

首先,在应用启动时初始化广告,而不是在实际想要显示广告时再进行初始化。然后保持广告预加载,这样当您想要在那时显示广告时,您将已经准备好广告,因此可以立即显示。

英文:

First of all initialize Ads on app launch, not at the time of when actually you want to show the ad. Then keep ads preloaded so when you want to show ads at that time you'll have the ad ready to show, so it'll be displayed immediately.

huangapple
  • 本文由 发表于 2023年6月13日 02:06:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459240.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定