You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android插页广告无法点击即显问题求助

解决插页广告自动展示问题,改为点击触发

Hey there! I see you're new to Android development (only 3 months in—nice work pushing through!) and you're stuck with interstitial ads popping up automatically instead of when users click list items. Let's break down what's going wrong and fix it step by step.

问题根源

Looking at your code, the core issue is right here in your AdListener implementation:

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        if (mInterstitialAd.isLoaded()) 
            mInterstitialAd.show(); // 这里广告一加载完成就自动弹出了!
    }
    /* public void onAdClosed() {
        mInterstitialAd.loadAd(new AdRequest.Builder().build());
    } */
});

Every time the interstitial finishes loading, you're immediately calling show()—that's why it keeps popping up every few seconds as ads reload. We need to move that show() call to the actual user click event.

修改步骤

  1. 移除自动展示逻辑: Delete the show() call inside onAdLoaded()—we only want ads to appear when the user takes action.
  2. 绑定广告到点击事件: Add the ad display logic to your onItemClick() method, so ads only show when a user taps a list item.
  3. 重新加载广告: Uncomment and complete the onAdClosed() callback to reload a new ad after the current one is closed, so you have an ad ready for the next click.

修改后的完整代码片段

First, update your AdListener configuration:

mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        super.onAdLoaded();
        // 广告加载完成后不再自动弹出,等待用户点击
    }

    @Override
    public void onAdClosed() {
        super.onAdClosed();
        // 广告关闭后立即加载下一个,确保下次点击有广告可用
        AdRequest request = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .build();
        mInterstitialAd.loadAd(request);
    }
});

Then modify the onItemClick() method in your AttractionAdapter to trigger the ad:

@Override
public void onItemClick(View view, int position) {
    if (!mItemClicked) {
        mItemClicked = true;
        // 先检查广告是否已加载完成,再展示
        if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
            mInterstitialAd.show();
        }
        // 跳转到详情页(如果想让用户看完广告再跳转,可把这段移到onAdClosed()里,记得保存position信息)
        View heroView = view.findViewById(android.R.id.icon);
        DetailActivity.launch(
                getActivity(), 
                mAdapter.mAttractionList.get(position).name, 
                heroView);
    }
}

额外小提示

  • If you want users to view the ad before navigating to the detail page, move the DetailActivity.launch() code into the onAdClosed() callback. Just make sure to store the clicked position in a class variable so you know which attraction to open later.
  • Add cleanup logic to avoid memory leaks when the fragment is destroyed:
@Override
public void onDestroy() {
    super.onDestroy();
    if (mInterstitialAd != null) {
        mInterstitialAd.destroy();
    }
}

That's it! Now your interstitial ads will only show when users tap list items, just like you wanted. Keep experimenting—you're doing great for only 3 months in!

内容的提问来源于stack exchange,提问作者Bhavesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:25:56