Skip to content

Instantly share code, notes, and snippets.

@first087
Created November 27, 2013 09:14
Show Gist options
  • Save first087/7672854 to your computer and use it in GitHub Desktop.
Save first087/7672854 to your computer and use it in GitHub Desktop.
Android Application : Load Ads (AdMob) Before Enable Feature
package com.ethanf.admob.enablefeature;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.google.ads.*;
import com.google.ads.AdRequest.ErrorCode;
public class ExActivity extends Activity {
private Context context;
private String tag;
private RelativeLayout LayoutAds;
private AdView adView;
private final String AD_UNIT_ID = "a00000000000000";
private boolean enableFeature;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ex);
// Context
context = this;
// LogTag
tag = getString(R.string.app_name);
// Matching view
LayoutAds = (RelativeLayout) findViewById(R.id.LayoutAds);
// Ads
// Ref.: https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals
// Create the adView
adView = new AdView(this, AdSize.BANNER, AD_UNIT_ID);
adView.setAdListener(new AdListener() {
public void onReceiveAd(Ad arg0) {
enableFeature = true;
}
public void onPresentScreen(Ad arg0) {
}
public void onLeaveApplication(Ad arg0) {
}
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
enableFeature = false;
}
public void onDismissScreen(Ad arg0) {
}
});
adView.loadAd(new AdRequest());
LayoutAds.addView(adView);
// ...
}
@Override
protected void onDestroy() {
if (adView != null) adView.destroy();
super.onDestroy();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment