英文:
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, "UnitID", adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
mInterstitialAd =null;
Toast.makeText(context, "ad loading failed", 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);
}
}}
and here's my on create method
@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();});
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论