Skip to content

Instantly share code, notes, and snippets.

@anzfactory
Created October 19, 2014 04:38
Show Gist options
  • Save anzfactory/f5dc47985c09c8b1400e to your computer and use it in GitHub Desktop.
Save anzfactory/f5dc47985c09c8b1400e to your computer and use it in GitHub Desktop.
Admobインタースティシャル広告さんぷる
public class InterstitialActivity extends Activity {
private InterstitialAd mInterstitial;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setup
setupInterstitialAd();
// load開始
loadInterstitialAd();
}
// なにかのボタン押した時とかに表示する場合
public void onClickButton(View v) {
showInterstitialAd();
}
private void setupInterstitialAd() {
mInterstitial = new InterstitialAd(this);
mInterstitial.setAdUnitId("ca-app-pub-xxxxxxxxxxxxxxxxxxxxx");
// リスナー設定
mInterstitial.setAdListener(new AdListener() {
/**
* インタースティシャル広告がとじたらコールされる
*/
@Override
public void onAdClosed() {
super.onAdClosed();
// 再度表示にそなえてロードしておく
// 再度ロードしないと、isLoaded()がfalseになるみたい
// どうやら、1回使いきりっていう感じの挙動。。。(;^ω^)
loadInterstitialAd();
}
});
}
private void loadInterstitialAd() {
AdRequest adRequestForInterstitial = new AdRequest.Builder().build();
mInterstitial.loadAd(adRequestForInterstitial);
}
private void showInterstitialAd() {
// 通信状態によっては、ロードが終わってないことがあるので
if (!mInterstitial.isLoaded()) {
return;
}
mInterstitial.show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment