Skip to content

Instantly share code, notes, and snippets.

  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save first087/9015062 to your computer and use it in GitHub Desktop.
Android Application : Load Ads (AdMob) Before Enable Feature (Google Play Services Library)
package com.ethanf.admob.enablefeature;
import android.content.Context;
import android.util.Log;
import android.view.View;
import com.google.android.gms.ads.*;
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);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
enableFeature = true;
}
@Override
public void onAdFailedToLoad(int errorCode) {
enableFeature = false;
}
});
adView.loadAd((new AdRequest.Builder()).build());
LayoutAds.addView(adView);
// ...
}
@Override
protected void onPause() {
if (adView != null) adView.pause();
super.onPause();
}
@Override
protected void onResume() {
if (adView != null) adView.resume();
super.onResume();
}
@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