求助:按下返回按钮时Interstitial ad(插屏广告)无法显示
Hey there! Let's break down why your interstitial ad isn't popping up when the back button is pressed, and how to fix it. First off, the code you shared is for a banner ad (AdView)—that's a completely different ad format from interstitial ads, so that's a critical mismatch to address. Let's walk through the correct implementation step by step:
1. Confirm SDK Setup First
Make sure you've properly integrated the Google Mobile Ads SDK into your project:
- Add the SDK dependency in your app-level
build.gradle:implementation 'com.google.android.gms:play-services-ads:22.6.0' - Add required metadata and permissions in
AndroidManifest.xml:<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <application> <meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/> </application>
Replace the APPLICATION_ID value with your own from the AdMob console.
2. Declare & Load the Interstitial Ad
In your Activity class, declare an InterstitialAd object (not an AdView) and load it when the Activity starts:
import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.InterstitialAd; import com.google.android.gms.ads.LoadAdError; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback; public class YourActivity extends AppCompatActivity { private InterstitialAd mInterstitialAd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_activity_layout); // Initialize Mobile Ads SDK MobileAds.initialize(this, initializationStatus -> {}); // Load interstitial ad AdRequest adRequest = new AdRequest.Builder().build(); // Use YOUR interstitial ad unit ID here (not the banner one!) InterstitialAd.load(this, "ca-app-pub-xxxxxxxxxxxxxxxx/zzzzzzzzzz", adRequest, new InterstitialAdLoadCallback() { @Override public void onAdLoaded(@NonNull InterstitialAd interstitialAd) { mInterstitialAd = interstitialAd; Log.d("AdStatus", "Interstitial ad loaded successfully"); } @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { mInterstitialAd = null; Log.e("AdError", "Ad failed to load: " + loadAdError.getMessage()); } }); }
Important: Replace the ad unit ID with your actual interstitial ad ID from AdMob. For testing, use the official test ID: ca-app-pub-3940256099942544/1033173712 to avoid policy violations.
3. Override Back Button Behavior
Now, override the onBackPressed() method to show the interstitial ad when the back button is pressed (only if the ad is loaded):
@Override public void onBackPressed() { if (mInterstitialAd != null) { mInterstitialAd.show(this); // Handle ad lifecycle events mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() { @Override public void onAdDismissedFullScreenContent() { // Proceed with back action after ad is closed super.onAdDismissedFullScreenContent(); mInterstitialAd = null; // Reset to load a new ad later YourActivity.super.onBackPressed(); } @Override public void onAdFailedToShowFullScreenContent(@NonNull AdError adError) { // If ad fails to show, fall back to normal back action super.onAdFailedToShowFullScreenContent(adError); mInterstitialAd = null; YourActivity.super.onBackPressed(); } }); } else { // No ad loaded, perform normal back navigation super.onBackPressed(); } } }
Key Notes to Avoid Issues
- Don't mix ad formats: The
AdViewin your layout is for banner ads—you can remove that from your XML if you're only using interstitial ads here. - Follow AdMob policies: Don't show interstitial ads too frequently (e.g., don't trigger it on every back press in a short time) to avoid policy violations.
- Test with test ads: Always use test ad units during development to ensure your implementation works without risking account issues.
That should get your interstitial ad showing when the back button is pressed! Let me know if you hit any snags along the way.
内容的提问来源于stack exchange,提问作者walidroid

