2016年6月9日 星期四

Add Interstitial Ad(插頁廣告)



匯入 com.google.android.gms.ads.*
宣告執行個體
建立插頁廣告,並指定 AdMob 廣告單元編號 (不能與橫幅廣告所用的編號重複)
再次提醒您,最好在應用程式的 Activity 內執行這些操作。


need dependency:
compile 'com.google.android.gms:play-services:8.1.0'

step1 prepare
// Create the InterstitialAd and set the adUnitId.
       mInterstitialAd = new InterstitialAd(this);
       // Defined in res/values/strings.xml
       mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));//ca-app-pub-3940256099942544/1033173712

Srtep2 set the action when the ad closed
mInterstitialAd.setAdListener(new AdListener() {
           @Override
           public void onAdClosed() {
              //do something here
           }
       });

Step3 Load the AD and set the after loaded action

if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
           AdRequest adRequest = new AdRequest.Builder().build();
           mInterstitialAd.loadAd(adRequest);
       }
mRetryButton.setVisibility(View.INVISIBLE);
       resumeGame(GAME_LENGTH_MILLISECONDS);


explain:
it is becasue load the AD need time,showwe set only finsih load,tham show the retry button or do other.


Step4 show it
if AD is load,than can show

if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
           mInterstitialAd.show();
       }