android 如何在插播广告前添加定时器?

mgdq6dx1  于 2023-02-10  发布在  Android
关注(0)|答案(1)|浏览(154)

我是新的android工作室,我有这个代码显示间隙。现在我想根据admob的政策显示间隙广告之前添加计时器。但不知道如何添加这个。有人能帮我做到这一点吗?

private void goMainScreen(int position) {
    if (mInterstitialAd != null) {
        mInterstitialAd.show(this);
        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdClicked() {
                super.onAdClicked();
            }

            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                startActivity(new Intent(MainActivity.this, SignsActivity.class));
                Pref.getInstance(MainActivity.this).setIndex(position);
            }

            @Override
            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                super.onAdFailedToShowFullScreenContent(adError);
                startActivity(new Intent(MainActivity.this, SignsActivity.class));
                Pref.getInstance(MainActivity.this).setIndex(position);
            }

            @Override
            public void onAdImpression() {
                super.onAdImpression();
            }

            @Override
            public void onAdShowedFullScreenContent() {
                super.onAdShowedFullScreenContent();
            }
        });

    } else {
        startActivity(new Intent(MainActivity.this, SignsActivity.class).putExtra("index", position));
        Pref.getInstance(MainActivity.this).setIndex(position);
    }

    adPosition = adPosition + 1;

}
8ljdwjyq

8ljdwjyq1#

要在显示插播广告之前添加计时器,您可以使用Android中的Handler对象延迟广告的显示。以下是如何修改代码的示例:

private void goMainScreen(int position) {
    if (mInterstitialAd != null) {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mInterstitialAd.show(MainActivity.this);
            }
        }, 5000); // 5000 milliseconds = 5 seconds

        mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
            @Override
            public void onAdClicked() {
                super.onAdClicked();
            }

            @Override
            public void onAdDismissedFullScreenContent() {
                super.onAdDismissedFullScreenContent();
                startActivity(new Intent(MainActivity.this, SignsActivity.class));
                Pref.getInstance(MainActivity.this).setIndex(position);
            }

            @Override
            public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                super.onAdFailedToShowFullScreenContent(adError);
                startActivity(new Intent(MainActivity.this, SignsActivity.class));
                Pref.getInstance(MainActivity.this).setIndex(position);
            }

            @Override
            public void onAdImpression() {
                super.onAdImpression();
            }

            @Override
            public void onAdShowedFullScreenContent() {
                super.onAdShowedFullScreenContent();
            }
        });

    } else {
        startActivity(new Intent(MainActivity.this, SignsActivity.class).putExtra("index", position));
        Pref.getInstance(MainActivity.this).setIndex(position);
    }

    adPosition = adPosition + 1;
}

在上面的代码中,我们使用Handler类的postDelayed方法将插入式广告的显示延迟5000毫秒(5秒)。如果愿意,您可以将此值调整为不同的持续时间。

相关问题