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

WebView图片下载完成弹窗点击OK后展示Interstitial Ad需求实现

Show Interstitial Ad When User Clicks "OK" on Download Complete Dialog

Great question! To make this happen, you’ll need to add a click listener to your dialog’s "OK" button that triggers the interstitial ad when tapped. Here’s a step-by-step solution tailored to your code:

Step 1: Initialize the Interstitial Ad

First, set up the interstitial ad in your Activity/Fragment (we’ll cover both legacy and latest SDK approaches since Google updated their ad APIs):

Option A: Legacy InterstitialAd (Pre SDK 19.0.0)

private InterstitialAd interstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout);

    // Initialize ad with your unique ad unit ID
    interstitialAd = new InterstitialAd(this);
    interstitialAd.setAdUnitId("YOUR_AD_UNIT_ID"); // Replace with your actual ID

    // Load the initial ad
    AdRequest adRequest = new AdRequest.Builder().build();
    interstitialAd.loadAd(adRequest);

    // Register your download complete receiver
    registerReceiver(downloadListener, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

Option B: Latest InterstitialAd (SDK 19.0.0+)

Google deprecated the old InterstitialAd class, so use this updated approach:

private InterstitialAd interstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_activity_layout);

    // Load ad using the new callback method
    loadInterstitialAd();

    // Register download receiver
    registerReceiver(downloadListener, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

// Helper method to reload ads after they're shown
private void loadInterstitialAd() {
    AdRequest adRequest = new AdRequest.Builder().build();
    InterstitialAd.load(this, "YOUR_AD_UNIT_ID", adRequest,
        new InterstitialAdLoadCallback() {
            @Override
            public void onAdLoaded(@NonNull InterstitialAd ad) {
                interstitialAd = ad;
                // Set callback to reload ad after it's closed
                ad.setFullScreenContentCallback(new FullScreenContentCallback() {
                    @Override
                    public void onAdDismissedFullScreenContent() {
                        loadInterstitialAd();
                    }

                    @Override
                    public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) {
                        interstitialAd = null;
                        loadInterstitialAd();
                    }
                });
            }

            @Override
            public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                interstitialAd = null;
                // Handle load failure if needed
            }
        });
}

Step 2: Modify the Dialog to Trigger Ad on OK Click

Update your BroadcastReceiver to add a click listener to the positive button:

BroadcastReceiver downloadListener = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        new MaterialDialog.Builder(context)
                .title("Download Completed")
                .content("Download Successfully Completed")
                .positiveText("OK")
                .onPositive((dialog, which) -> {
                    // Show ad when OK is clicked
                    if (interstitialAd != null) {
                        // For legacy SDK: Check if ad is loaded first
                        if (interstitialAd.isLoaded()) {
                            interstitialAd.show();
                            // Reload ad for next use
                            interstitialAd.loadAd(new AdRequest.Builder().build());
                        } else {
                            Toast.makeText(context, "Ad not ready yet", Toast.LENGTH_SHORT).show();
                            interstitialAd.loadAd(new AdRequest.Builder().build());
                        }
                        
                        // For latest SDK: Just call show() (it checks load status internally)
                        // interstitialAd.show(YourActivityName.this);
                    } else {
                        Toast.makeText(context, "Ad not ready yet", Toast.LENGTH_SHORT).show();
                        loadInterstitialAd();
                    }
                })
                .show();
    }
};

Step 3: Clean Up Resources

Avoid memory leaks by unregistering the receiver when your activity is destroyed:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (downloadListener != null) {
        unregisterReceiver(downloadListener);
    }
}

Key Notes

  • Test First: Use Google’s test ad units (e.g., ca-app-pub-3940256099942544/1033173712 for interstitial ads) to avoid policy violations.
  • Update Dependencies: Ensure you have the latest SDK in your build.gradle:
    implementation 'com.google.android.gms:play-services-ads:22.4.0' // Check for the latest version
    
  • Handle Failures: Add logic to gracefully handle ad load failures so users get clear feedback.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.28 03:58:59