Admob Interstitial Preloading - android

I'm implementing Admob interstitial ads in a button. But it take 3-4 seconds to load the ads, user wrongly click button again and app get crush. How to preload interstitial ads or load instantly when press the button. Here is my code.
img_ply.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
mAdView = (AdView) findViewById(R.id.adView);
mAdView.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdUnitId(getResources().getString(R.string.admob_intertestial_id));
mInterstitial.loadAd(new AdRequest.Builder().build());
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
if (mInterstitial.isLoaded()) {
mInterstitial.show();
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
super.onAdClosed();
finish();
Intent inttv = new Intent(SingleChannelActivity.this, TvPlay.class);
inttv.putExtra("url", ChannelUrl);
startActivity(inttv);
}
});
}else{
super.onAdLoaded();
}
}
});
}
});

You should put Interstitial load method into onCreate() function of view, not in onClick event of the button !
So, there will be only mInterstitial.show() method in onClick() event.
You must load the inters ads onCreate() and inters ads will be ready whenever you need..

Related

Admob interstitial ads are not closing

I'm showing interstitial ad in my app and ads are loading and displaying perfectly but the issue is that ads are not closing when I click cross button on back button I have to quit my app to close them
I created a new project and checked the ads in new project they are working fine and closing on back button but in my main app ads are not closing once they displayed to user
this is my Admob class
public class Admob {
public static InterstitialAd mInterstitial; // Interstital
private static AdView mAdView; // banner
public static void createLoadInterstitial(final Context context, View view)
{
mInterstitial = new InterstitialAd(context);
mInterstitial.setAdUnitId(context.getResources().getString(
R.string.admob_showIntersitial_ad_unit_id));
mInterstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
showInterstitial();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
// Called when an ad leaves the app (for example, to go to the
// browser).
super.onAdLeftApplication();
}
});
loadInterstitial();
}
public static void loadInterstitial() {
mInterstitial.loadAd(new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/1033173712
build());
}
public static void showInterstitial() {
if (mInterstitial.isLoaded()) {
mInterstitial.show();
}
}
public static void createLoadBanner(final Context context, View view) {
mAdView = (AdView) view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice("").//ca-app-pub-3940256099942544/6300978111
build();
mAdView.loadAd(adRequest);
mAdView.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
}
});
}
}
This is how I show them in my activity
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
Admob.createLoadInterstitial(this,null);
I have initialize this in Mainfest also
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
I saw same questions on stackoverflow but none of them is answered the solution so any help will be really appreciated
If you are using Webiew check if there is webview is paused on ad shown

Show interstitial ads when clicking on any of four buttons

I have a layout with four buttons. When the user clicks on one of the buttons, I want to show interstitial ads .
My code below works when I click on one of the four buttons, but when I click again, the interstitial ads don't show.
For example:
BUTTON1BUTTON2BUTTON3BUTTON4
When I click for the first time on BUTTON1 the interstitial ads show; when I want to click again on one of the buttons (BUTTON2, for example), the interstitial ads are not showing at all.
How can I get the interstitial ads to show each time I click the button?
InterstitialAd mInterstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.interstitialAd));
mInterstitialAd.loadAd(adRequest);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 1 function
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 2 function
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 3 function
}
});
btn4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
});
//button 4 function
}
});
After displaying an InterstitialAd, you should request a new one with a new AdRequest:
So, one option is add following lines to your every ClickListener (not discussing how to improve the quality... just how to fix it):
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
mInterstitialAd.loadRequest(new AdRequest.Builder().build());
}
Could you please test and share the results (you may need to wait until a new AD is loaded before clicking on next button).

Admob Add shown in Delay Time

public class MainActivity extends Activity {
AdView adView;
InterstitialAd interstitialAd;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adView = (AdView) findViewById(R.id.adView);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-3500425325579414/2869090680");
AdRequest adRequest = new AdRequest.Builder().addTestDevice(
"BD9386F17F7DE795B86B5BBBEDDF1095").build();
adView.loadAd(adRequest);
interstitialAd.loadAd(adRequest);
interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
Thread timerThread = new Thread() {
#Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
?????????????????????
}
}
};
timerThread.start();
}
}
I want to show my Admob add after my selected time.
I want to use a Thread to show my add in dealy.
But how i complite this i could not do it.
Even i tryed this
Thread timerThread = new Thread() {
#Override
public void run() {
// TODO Auto-generated method stub
super.run();
try {
sleep(5000);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
}
}
});
}
}
};
timerThread.start();
But this is not working.
How i can do it in programatically.
The problem I see is that you are setting the Adlistener after a delay instead of showing the ad after a delay.
Also you don't need to create a Thread for doing that, you can try this for example:
interstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
MainActivity.this.interstitialAd.show();
}
}, 5000);
}
});
Put this in your MainActivity onCreate method and get rid of the timerThread. This way you'll have your interstitial configured that it will show up 5 sec after the ad is loaded.
Note: From user a user experience point of view that is not such a good idea because you'll most likely end up annoying your users. The best practice is to show ads at natural breaks, like "level completed" etc.
You DON'T want to show your interstitial ads at random (delayed or not) time within your app. Unless
You want really bad user ratings
You want Admob to disable your account.
Instead you should pick a suitable break point in your app and show the ad then.

Admob Interstitial ad is not clickable in android app exit

I am trying to show admob interstitial ad at the end of the application when user presses back button from the main activity. Ad is shown, but the ad is not clickable. My code is
I put loading part at the activity onCreate :
interstitial = new InterstitialAd(getApplicationContext());
interstitial.setAdUnitId("Ad UNIT ID");
// Create ad request.
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
and in OnBackPressed :
#Override
public void onBackPressed() {
super.onBackPressed();
displayInterstitial();
}
protected void displayInterstitial() {
Log.d(TAG, "start displayInterstitial()");
if (null != interstitial && interstitial.isLoaded()) {
Log.d(TAG, "displayInterstitial() loaded");
interstitial.show();
}
Log.d(TAG, "end displayInterstitial()");
}
Can anyone please help me to fix this issue?
Simplest way would be:
interstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
finish();
}
});
However, in my opinion it is better to stay at your app for a sec before app finishes. That way it is more clear that this ad was still part of your app:
interstitial.setAdListener(new AdListener() {
#Override
public void onAdClosed() {
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
finish();
}
}, 1000);
}
});
BTW: about the comment of calling MainActivity.super.onBackPressed(); inside the adlistener's onAdClosed: I tested it and threw an IllegalStateException. I guess that is because the moment when onBackPressed is called is too late already. But well it is anyway not needed, so forget about it. The super onBackPressed method itself is accessible from the AdListener though and will compile just fine ;-)
#Override
public void onBackPressed() {
Log.d(TAG, "onBackPressed Called");
displayInterstitial();
//finish();
super.onBackPressed();
}

InterstitialAd should load after every exposure

I need to use InterstitialAd in my app. But the problem is that after first exposure, before showing I am creating ad, call loadAd method with request, and than show (calling show method)it in the right moment. Everything seems to work good, but when I try to call show method next time it doesn't work because of isLoaded returns false.
Is there any way to load ad only once.
EDIT
Thx for answer, but the main problem is that loadAd is network connection, but the google suggest not to perform internet tasks in main thread, so what the f..k it is loading ad from the internet in main ui thread.It causes lags in my app. They are contradicting themselves.
You'd have to reload the ad anytime it's closed. I define a single method "reloadInterstitial()" and invoke it within the onCreate() method. It works great. All you have to do is call displayInterstitial() whenever you wish you to show the ad:
public class MyActivityClass extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("*****");
reloadInterstitial();
// Create ad request
AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
}
public void displayInterstitial(){
if(interstitial != null && interstitial.isLoaded())
interstitial.show();
}
private void reloadInterstitial(){
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("*****");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("****");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}
private void loadInterCallBacks(){
interstitial.setAdListener(new AdListener() {
#Override
public void onAdLoaded() {
// TODO Auto-generated method stub
super.onAdLoaded();
}
#Override
public void onAdFailedToLoad(int errorCode) {
// TODO Auto-generated method stub
super.onAdFailedToLoad(errorCode);
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("xxxxxxxxxxx");
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
loadInterCallBacks();
}
#Override
public void onAdOpened() {
// TODO Auto-generated method stub
super.onAdOpened();
}
#Override
public void onAdClosed() {
// TODO Auto-generated method stub
super.onAdClosed();
interstitial = new InterstitialAd(MyActivityClass.this);
interstitial.setAdUnitId("xxxxxx");
loadInterCallBacks();
// Begin loading your interstitial
interstitial.loadAd(new AdRequest.Builder().build());
}
#Override
public void onAdLeftApplication() {
// TODO Auto-generated method stub
super.onAdLeftApplication();
}
});
}

Categories

Resources