Reputation: 536
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) :
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?
Upvotes: 4
Views: 1666
Reputation: 956
@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 :)
Upvotes: 0
Reputation: 20196
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.
Upvotes: 1