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
Related
For some reason facebook interstitialad does not display close button in my android app. I am followed all the steps from: https://developers.facebook.com/docs/audience-network/android/interstitial
However, using back button it does close the ad correctly. But I need to have close button displayed, any help?
First image:
Second Image:
Following is code which is responsible for this AD:
public class MyActivity extends SherlockFragmentActivity implements
SearchView.OnSuggestionListener, InterstitialAdListener {
private InterstitialAd interstitialAd;
boolean adDismissed;
private void loadInterstitialAd() {
interstitialAd = new InterstitialAd(this, "my_id");
interstitialAd.setAdListener(this);
interstitialAd.loadAd();
}
#Override
public void onAdLoaded(Ad ad) {
// Ad is loaded and ready to be displayed
// You can now display the full screen add using this code:
interstitialAd.show();
}
#Override
public void onAdClicked(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onInterstitialDismissed(Ad arg0) {
// TODO Auto-generated method stub
adDismissed = true;
}
#Override
public void onInterstitialDisplayed(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onError(Ad ad, AdError error) {
// Ad failed to load
}
#Override
public void onStart() {
super.onStart();
if(!adDismissed)
loadInterstitialAd();
}
}
I try to integrate google Analytics to my Application but it doesn´t work.
This is my Activity:
public class InhaltsverzeichnisActivity extends Activity {
private SharedPreferencesManager prefs; //added
private InterstitialAd Interstitial;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inhaltsverzeichnis);
prefs = new SharedPreferencesManager(this); //get SharedPreferencesManager instance
int t = prefs.retrieveInt("theme", 0); //get stored theme, zero is default
ThemeUtils.setTheme(t); //Set the stored theme, will default to Black
Tracker t1 = ((AnalyticsSampleApp)this.getApplication()).getTracker(TrackerName.APP_TRACKER);
t1.setScreenName("Inhaltsverzeichnis");
t1.send(new HitBuilders.AppViewBuilder().build());
Interstitial = new InterstitialAd(this);
Interstitial.setAdUnitId("ca-app-pub-XXXXXXXXXXXXX/XXXXXXXXXX");
AdRequest adRequest = new AdRequest.Builder().build();
Interstitial.loadAd(adRequest);
;
Interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
Interstitial.show();
}
});
}
protected void displayInterstitial() {
// TODO Auto-generated method stub
}
public void onDismissScreen() {
// TODO Auto-generated method stub
}
public void onFailedToReceiveAd() {
// TODO Auto-generated method stub
}
public void onLeaveApplication() {
// TODO Auto-generated method stub
}
public void onPresentScreen() {
// TODO Auto-generated method stub
}
#Override
protected void onStart() {
super.onStart();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStart(this);
}
#Override
protected void onStop() {
super.onStop();
GoogleAnalytics.getInstance(InhaltsverzeichnisActivity.this).reportActivityStop(this);
}
If I put the OnStart and OnStop method over the Interstitial Ad , Analytics works, but the Interstitial Ad goes crazy and appears every second again,
hope anyone can help me to solve this problem thanks.
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();
}
});
}
Since I couldn't find any documentation about this methods I couldn't find an answer.
What is the purpose of these methods?
I added this AdRequest as and my logcat went crazy. Is that wrong?
#Override
public void onDismissScreen(Ad arg0) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
AdRequest adRequest = new AdRequest();
interstitial.loadAd(adRequest);
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
These are the callback methods, using these methods you can check that what is the status of the ad.
For example if the ad is not recived, onFailedToReceiveAd() will be called.
I wish to remove the ad if they are clicked once.
I implemented the following code.
I also added android:launchMode="singleInstance"
in the manifest and android:alwaysRetainTaskState="true" in the activity section of the manifest.
But when i click on the ad and return to the app the ad is still showing .
My code is as follows.
public class MainActivity extends Activity implements AdListener{
adView = (AdView)findViewById(R.id.ad);
adView.loadAd(new AdRequest());
// my code
#Override
public void onDismissScreen(Ad arg0) {
if (adView != null) {
adView.destroy();
}
// TODO Auto-generated method stub
}
#Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
// TODO Auto-generated method stub
}
#Override
public void onLeaveApplication(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPresentScreen(Ad arg0) {
// TODO Auto-generated method stub
}
#Override
public void onReceiveAd(Ad arg0) {
// TODO Auto-generated method stub
}
}
adView.setAdListener(this); /* first you should add listener register */
and for dismiss screen
public void onDismissScreen(Ad p1)
{
yourlayout.removeView(adView); // your layout should me remove here
}