4

I was trying to add Admob Interstitial to my Android App which is Displayed when someone leaves the Activity.

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class Banner extends Activity {

/**
 * Your ad unit id, you must replace it with your actual ad unit id Which
 * you can generate from Admob website
 * 
 */
private static final String AD_UNIT_ID = "MY ADD UNIT ID";
private static final String TAG = "ExampleActivity";
private InterstitialAd iAd;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.banner);

    iAd = new InterstitialAd(this);
    iAd.setAdUnitId(AD_UNIT_ID);

    iAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            Log.d(TAG, "onAdLoaded");
            Toast.makeText(Banner.this, "Ad loaded successfully",
                    Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            String errorMessage = String.format("Failed to load add : "
                    + getErrorReason(errorCode));
            Log.d(TAG, errorMessage);
            Toast.makeText(Banner.this, errorMessage,
                    Toast.LENGTH_SHORT).show();
        }
    });

    loadInterstitial();
}

public void loadInterstitial() {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice(
                    "You can add you device id here, run code once and get id from logs")
            .build();

    iAd.loadAd(adRequest);
}

public void showInterstitial() {
    if (iAd.isLoaded()) {
        iAd.show();
    } else {
        Log.d(TAG, "Interstitial ad is not loaded yet");
    }
}

/**
 * Gets a string error reason from an error code
 * 
 * @param errorCode
 * @return
 */
private String getErrorReason(int errorCode) {

    String errorReason = "unknown error";

    switch (errorCode) {
    case AdRequest.ERROR_CODE_INTERNAL_ERROR:
        errorReason = "internal error";
        break;
    case AdRequest.ERROR_CODE_INVALID_REQUEST:
        errorReason = "invalid request";
        break;
    case AdRequest.ERROR_CODE_NETWORK_ERROR:
        errorReason = "network Error";
        break;
    case AdRequest.ERROR_CODE_NO_FILL:
        errorReason = "no fill";
        break;
    }
    return errorReason;
}

@Override
protected void onDestroy() {
    showInterstitial();
    super.onDestroy();
}

}

The Interstitial loads successfully but I get an empty screen like this (I waited for 1-2 minutes but still no Ad shows) :

enter image description here

I double checked my AD_UNIT_ID. What could be the problem? Is this normal? I am still in development phase so will this be rectified when I upload my app on Play Store?

Rushil Sablania
  • 526
  • 4
  • 15
  • correct me if I'm wrong, you are trying to display an ad when users are attempting to exit the activity. In that case you should have used onBackPressed instead of onDestroy, and super.onBackPressed() should be put onAdClosed() in AdListener. – Hendra Anggrian Jan 08 '16 at 07:17

2 Answers2

1

You are never going to get this to work.

At the point at which your Activity is being destroyed there is no context for the interstitial. Instead find a natural break point in your app and show the interstitial then.

William
  • 20,150
  • 8
  • 49
  • 91
0

@Rushil if Banner is not your base activity then you can start Banner Activity using startActivityForResult() method like this

Intent intent = new Intent(MainActivity.this, Banner.class);
startActivityForResult(intent,0);

Then show ad in OnActivityResult function

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==0||resultCode==Activity.RESULT_OK)
    {
        if (iAd.isLoaded()) 
        {
         iAd.show();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);

}

Hope this helps :)

Muhammad Hamza Shahid
  • 956
  • 1
  • 15
  • 23