gpt4 book ai didi

java - 为什么最近调用 onAdDismissedFullScreenContent ?

转载 作者:行者123 更新时间:2023-12-05 00:01:53 26 4
gpt4 key购买 nike

我创建了一个功能来在显示另一个 Activity 之前显示插页式广告。
它正在工作,但是 onAdDismissedFullScreenContent最近在关闭广告后 2-3 秒被调用。它会导致前一个屏幕处于待机状态。
有机会重新单击按钮以显示所需的 Activity 。
我真的不明白如何解决这个问题。

这是我的代码:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AdManager.showInterstitial(MainActivity.this, () -> {
Intent i = new Intent(MainActivity.this, MainActivity2.class);
startActivity(i);
});
}
});
AdManager.loadInterstitial(this);
}
}

这是应用程序类:

public class App extends Application {
private static Context mContext;


@Override
public void onCreate() {
super.onCreate();
mContext = getApplicationContext();
AdManager.Init(this);
}

public static Context getContext() {
return mContext;
}
}
AdManager 类:
public class AdManager {

private static InterstitialAd mInterstitialAd;
private static boolean loading, showing;


public static void Init(Context context) {

MobileAds.initialize(context, initializationStatus -> {
loadInterstitial(context);
});
}


public static void loadInterstitial(Context context) {

if (context == null)
context = App.getContext();

Context finalContext = context;

if (mInterstitialAd != null) {
Toast.makeText(context,"already loaded",Toast.LENGTH_LONG).show();
return;
}
if (loading) {
Toast.makeText(context,"already another request is processing",Toast.LENGTH_LONG).show();
return;
}
Toast.makeText(context,"requesting interstitial",Toast.LENGTH_LONG).show();

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

InterstitialAd.load(context, "Interstial Ad Unit ID", adRequest, new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;
Toast.makeText(finalContext,"ad loaded",Toast.LENGTH_LONG).show();
loading = false;
}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
mInterstitialAd = null;
loading = false;
Toast.makeText(finalContext,"ad failed to load",Toast.LENGTH_LONG).show();
}

});
}

public static void showInterstitial(Activity activity, @NonNull AdListener listener) {

if (showing) {
Toast.makeText(activity,"already showing",Toast.LENGTH_LONG).show();
listener.onCompleted();
return;
}

if (mInterstitialAd != null) {
showNow(activity, listener);

} else {
Toast.makeText(activity,"mInterstitialAd is null",Toast.LENGTH_LONG).show();
listener.onCompleted();
loadInterstitial(activity.getApplicationContext());
}

}

private static void showNow(Activity activity, AdListener listener) {

if (mInterstitialAd != null && !showing) {
showing = true;
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
Toast.makeText(activity,"Ad dismissed",Toast.LENGTH_LONG).show();
mInterstitialAd = null;
listener.onCompleted();
showing = false;
// times to load an new interstitial ad content
loadInterstitial(activity.getApplicationContext());
}

@Override
public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
Toast.makeText(activity,"AdFailedToShowFullScreenContent",Toast.LENGTH_LONG).show();
mInterstitialAd = null;
listener.onCompleted();
showing = false;

// times to load an new interstitial ad content
loadInterstitial(activity.getApplicationContext());
}


});
// Now show the ad
Toast.makeText(activity,"call to show ad",Toast.LENGTH_LONG).show();
mInterstitialAd.show(activity);
} else {
Toast.makeText(activity,"either mInterstitialAd is null or ad already is showing",Toast.LENGTH_LONG).show();
listener.onCompleted();
}
}

public interface AdListener {
void onCompleted();
}

}

请帮忙。

最佳答案

延迟问题

你真的无能为力。问题来自admob。这会发生。但是,要了解更多信息,我们可以在 Google 问题跟踪器上发布一个问题。
按键问题

我不认为这是一项艰巨的任务。只需按照以下步骤操作:

  • 创建 ProgressBar在按钮顶部的 xml 布局中并将其可见性设置为 gone
  • 当点击按钮并显示广告时,您可以将按钮设置为不可见,并且可以将进度条设置为不可见。
  • 然后在延迟之后,另一个 Activity 打开,对来自 NullPointerException 的 Activity 没有任何损害。
  • 关于java - 为什么最近调用 onAdDismissedFullScreenContent ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72139334/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com