How To Show Interstitial Ads in Every 20 Seconds in Android Studio Java

 


How To Show Interstitial Ads in Every 20 Seconds in Android Studio Java:

In this blog we describe How to Repeat Fullscreen Ads in android studio java and How To Show Interstitial Ads in Every 20 Seconds in android studio java.

                                        YouTube Video Tutorial



Step 1 : Add Internet Permission and Admob Metadata

Copy this code and paste into your manifest.xml
<uses-permission android:name="android.permission.INTERNET"/>

<!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>

Step 2 : Implement Admob Library

Copy this code and paste into your build.gradle(Module:app)
// Admob Ads Library
implementation 'com.google.android.gms:play-services-ads:23.1.0'

Step 3 : Initialize Admob

Copy this code and paste into your MainActivity.java in onCreate method
// Admob Initialize
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();

Step 4 : Fullscreen Ads Java Code

Copy this code and paste this code into your MainActivity.java above onCreate method
// Step 1 - Implement FullScreen/Interstitial Ads
InterstitialAd mInterstitialAd;

Copy this code and paste this code into your MainActivity.java bellow onCreate method
// Step 2 - Implement FullScreen/Interstitial Ads ======================================== START
private void loadFullscreenAd() {

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

InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;

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

}

@Override
public void onAdDismissedFullScreenContent() {
mInterstitialAd = null;
loadFullscreenAd();
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
mInterstitialAd = null;
}

@Override
public void onAdImpression() {
}

@Override
public void onAdShowedFullScreenContent() {

}
});

}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}

});

}
// Step 2 - Implement FullScreen/Interstitial Ads ========================================== END

Copy this code and paste this code into your MainActivity.java in onCreate method
// Call method
loadFullscreenAd();


Step 5 : Repeat Fullscreen ads Java Code

Copy this code and paste into your MainActivity.java above onCreate method
// Step 1 - Repeat FullScreen/Interstitial Ads
Handler handler = new Handler(Looper.getMainLooper());

Copy this code and paste into your MainActivity.java bellow onCreate method
// Step 2 - Repeat FullScreen/Interstitial Ads  ========================================== START
Runnable runnable = new Runnable() {
@Override
public void run() {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
}
handler.postDelayed(runnable, 20000);
}
};
// Step 2 - Repeat FullScreen/Interstitial Ads ============================================= END

Copy this code and paste into your MainActivity.java in onCreate method
// Step 3 - Repeat FullScreen/Interstitial Ads
runnable.run();

Copy this code and paste into your MainActivity.java bellow onCreate method
// Step 4 - Repeat FullScreen/Interstitial Ads
@Override
public void onBackPressed() {
handler.removeCallbacks(runnable);
super.onBackPressed();
}
Full Java Code 
package com.example.admobads;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd;
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback;

public class MainActivity extends AppCompatActivity {

InterstitialAd mInterstitialAd;
Handler handler = new Handler(Looper.getMainLooper());
RewardedAd rewardedAd;
private final String TAG = "MainActivity";
RewardedInterstitialAd rewardedInterstitialAd;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
// Admob Initialize
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
})
.start();
// Call method
loadFullscreenAd();
// Step 3 - Repeat FullScreen/Interstitial Ads
runnable.run();

setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;


});



}

// Implement FullScreen/Interstitial Ads ======================================== START
private void loadFullscreenAd() {

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

InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
mInterstitialAd = interstitialAd;

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

}

@Override
public void onAdDismissedFullScreenContent() {
mInterstitialAd = null;
loadFullscreenAd();
}

@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
mInterstitialAd = null;
}

@Override
public void onAdImpression() {
}

@Override
public void onAdShowedFullScreenContent() {

}
});

}

@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
super.onAdFailedToLoad(loadAdError);
}

});

}
// Step 2 - Implement FullScreen/Interstitial Ads ========================================== END
// Step 2 - Repeat FullScreen/Interstitial Ads ========================================== START
Runnable runnable = new Runnable() {
@Override
public void run() {
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
}
handler.postDelayed(runnable, 50000);
}
};
// Step 2 - Repeat FullScreen/Interstitial Ads ============================================= END

// Step 4 - Repeat FullScreen/Interstitial Ads
@Override
public void onBackPressed() {
handler.removeCallbacks(runnable);
super.onBackPressed();


}
}