As timing hum interstitial ad? - android

Example : What I want When you open the application, the ad appears within 5 seconds , if different , an exhibition will be canceled and the application Proceed paragraph the main activity.the code I am using is this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitialAd = new InterstitialAd(Main.this);
interstitialAd.setAdUnitId(getString(R.string.adMobInter));
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(getString(R.string.adMob_test))
.build();
interstitialAd.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
displayInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
onMain();
}
#Override
public void onAdClosed() {
onMain();
}
});
}
public void displayInterstitial() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
public void onMain() {
setContentView(R.layout.main);
}

Related

android studio: Interstitial Ad not show in splash screen activity

I'm trying to integrate Interstitial Ad in splash screen activity by using this tutorial .. but the ad not loading.
can anyone tell me where is the problem please?
thanks in advance
here is my code :
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
mInterstitialAd.setAdListener(new AdListener()
{
#Override
public void onAdLoaded() {
if (!interstitialCanceled) {
waitTimer.cancel();
mInterstitialAd.show();
}
}
#Override
public void onAdFailedToLoad(int errorCode) {
startHomeMain();
}
});
waitTimer = new Timer();
waitTimer.schedule(new TimerTask() {
#Override
public void run() {
interstitialCanceled = true;
SplashScreenActivity.this.runOnUiThread(new Runnable() {
#Override
public void run() {
startHomeMain();
}
});
}
}, 5000);
} // end of onCreate implementation.

Android Interstitial Ad not loading

I'm trying to load interstitial ad on the tablet, with the test ad id. On output ad is not showing, but I'm getting the callback on "onAdLoaded".
This is my code:-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ad_activity);
mPublisherInterstitialAd = new PublisherInterstitialAd(this);
mPublisherInterstitialAd.setAdUnitId("/6499/example/interstitial");
mPublisherInterstitialAd.loadAd(newPublisherAdRequest.Builder().build());
mPublisherInterstitialAd.setAdListener(new AdListener(){
#Override
public void onAdLoaded() {
Log.d("AD ","LOADED");
}
#Override
public void onAdFailedToLoad(int i) {
Log.d("AD ","FAILED");
}
});
}
Try this
public void inrequestadd() {
mInterstitial = new InterstitialAd(MainActivity.this);
mInterstitial.setAdUnitId("Your ID");
mInterstitial.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
}
#Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdLoaded() {
super.onAdLoaded();
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
});
}
Here is how I handle this kind of code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initAds();
}
private void initAds() {
MobileAds.initialize(this, "your app id");
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("your ad unit ID");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
Also in the manifest file:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="your app id"/>
Next, depending on the logic of your app, you would have something like:
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
System.out.println("The interstitial wasn't loaded yet. Do some other action");
}

Admob Reward Video Ad

This is my reward video code. What i want to do is a button with every click loads a reward video. The problems that faced me are, first the reward video loading takes much time to load and second the reward video sometimes is requested only once so what is the right way to start a reward video on every click with no delay?
rewardedVideoAd = MobileAds.getRewardedVideoAdInstance(MainActivity.this);
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917", new AdRequest.Builder().build());
rewardedVideoAd.setRewardedVideoAdListener(new RewardedVideoAdListener() {
#Override
public void onRewarded(RewardItem reward) {
}
#Override
public void onRewardedVideoAdLeftApplication() {}
#Override
public void onRewardedVideoAdClosed() {}
#Override
public void onRewardedVideoAdFailedToLoad(int errorCode) {
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {}
#Override
public void onRewardedVideoAdOpened() {}
#Override
public void onRewardedVideoStarted() {
rewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",new AdRequest.Builder().build());
}
});
and this is my button on click code
if (rewardedVideoAd.isLoaded()){
rewardedVideoAd.show();
}
i can shows video reward ads (only test ads) using below code.
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
private InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, getResources().getString(R.string.app_id));
// Use an activity context to get the rewarded video instance.
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
//getResources().getString(R.string.Video_reward_ad_unit_id)
mRewardedVideoAd.setRewardedVideoAdListener(this);
findViewById(R.id.btVideo).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mRewardedVideoAd.isLoaded()) {
mRewardedVideoAd.show();
}
}
});
mInterstitialAd = new InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.banner_ad_unit_id));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.show();
}
});
findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
}
#Override
protected void onStart() {
loadTestRewardedVideoAd();
MobileAds.setAppMuted(true);
super.onStart();
}
private void loadTestRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-3940256099942544/5224354917",
new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {
Log.i("onRewardedVideoAdLoaded", "Ad Loaded");
}
#Override
public void onRewardedVideoAdOpened() {
Toast.makeText(this, "Ad OPEPED Now ", Toast.LENGTH_SHORT).show();
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
}
#Override
public void onRewarded(RewardItem reward) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
Log.i("onRewardedVideoAdLeft", "END");
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
Log.i("onRewardedVideoAdLeft", "END");
}
but this is only works with test device not on real device.

build AdMob Interstitial ad again after 60 seconds

Currently, I am using the code which is displaying AdMob Interstitial ad after 20 seconds and when the ad is closed I want to show another ad after 60 seconds, this is the code currently I am using for the first ad to load after 20 seconds
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
interAd = new InterstitialAd(MainActivity.this);
interAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
interAd.loadAd(adRequest);
interAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
interAd.show();
}
});
interAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Code to be executed when the interstitial ad is closed.
Log.i("Ads", "onAdClosed");
}
});
}
} , 20000);
i have tried this method from AdMob tutorial it load the ads but after every second
interAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
// Load the next interstitial.
interAd.loadAd(new AdRequest.Builder().build());
}
});
You should attach you Ad's lifecycle to your Activity's lifecycle so you can start/stop showing adds just when your app is visible to the user.
Create a global variables for you Ad and Handler
private InterstitialAd mInterstitialAd;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
#Override
public void run() {
// Wait 60 seconds
mHandler.postDelayed(this, 60*1000);
// Show Ad
showInterstitial();
}
};
then...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the InterstitialAd and set the adUnitId
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
#Override
protected void onStart() {
super.onStart();
// Start showing Ad in every 60 seconds
//when activity is visible to the user
mHandler = new Handler();
//mHandler.post(mRunnable);
// Run first add after 20 seconds
mHandler.postDelayed(mRunnable,20*1000);
}
protected void onStop() {
super.onStop();
// Stop showing Ad when activity is not visible anymore
mHandler.removeCallbacks(mRunnable);
}
private void loadInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.setRequestAgent("android_studio:ad_template").build();
mInterstitialAd.loadAd(adRequest);
}
private InterstitialAd newInterstitialAd() {
InterstitialAd interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
interstitialAd.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
mNextLevelButton.setEnabled(true);
}
#Override
public void onAdFailedToLoad(int errorCode) {
mNextLevelButton.setEnabled(true);
}
#Override
public void onAdClosed() {
// Reload ad so it can be ready to be show to the user next time
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
});
return interstitialAd;
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and reload the ad.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
mInterstitialAd = newInterstitialAd();
loadInterstitial();
}
}
Make a new thread, then add a sleep time of 60000 milliseconds before showing your ad back. It will allow for async working.
Example, add this code in the onAdClosed() :
new Thread(new Runnable(){
public void run(){
Thread.sleep(60000);
// Show your ad here
}
}).start();

Add an interstitial admob ads without button pressed

I want to add an interstitial ads by admob without press any button but
it does not appear can you help I tried below codes:
...
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
mNewGameButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});
beginPlayingGame();
}
private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();
mInterstitialAd.loadAd(adRequest);
}
private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}
...
While loading an add you must add one device id that you are using for testing . while u r running this code see ur logcat to see the device id .. Copy and paste that device id into add test device and rerun u will start seeing add
public class MainActivity extends ActionBarActivity {
InterstitialAd mInterstitialAd;
Button mNewGameButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEViCE_ID").build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});
requestNewInterstitial();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
beginPlayingGame();
}
the true answer here i found it
interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.interstitialAd.show();
}
}, 5000);
}} );

Categories

Resources