2017-03-04 15 views
1

私は既存のAndroidアプリケーション(ゲームではない)のコードを編集しています。一定の数の画面や操作が行われた後にインタースティシャル広告を表示したい(Googleガイダンスを尊重する)。xアクション後にインタースティシャル広告を読み込む方法

これは私が通常私のインタースティシャル広告をロードする方法である:あなたが示さなければならない場合は、それをインクリメントする方法、およびチェックを行い、intを使用して、アクションを追跡

// Prepare the Interstitial Ad 
interstitial = new InterstitialAd(News_Detail.this); 

// Insert the Ad Unit ID 
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id)); 
AdRequest adRequest = new AdRequest.Builder().build(); 

// Load ads into Interstitial Ads 
interstitial.loadAd(adRequest); 

// Prepare an Interstitial Ad Listener 

interstitial.setAdListener(new AdListener() { 
     public void onAdLoaded() { 
      // Call displayInterstitial() function 
      displayInterstitial(); 
     } 
    }); 

public void displayInterstitial() { 
    // If Ads are loaded, show Interstitial else show nothing. 

new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      //Your code to show add 
      if (interstitial.isLoaded()) { 
       interstitial.show(); 
      } 
     } 
    }, 20000); 
    } 

答えて

2

を広告。はいの場合は、広告を表示してリセットします。

private static int x = 0; 

public static void incrementActions() { 
    x++; 
    if(x >= 5) { 
     x = 0; 
     displayInterstital(); 
    } 
} 

例えばMainActivityに入れて、MainActivity.incrementActions()のように呼び出します。 また、displayInterstital()メソッドを静的にするか、メソッドにそのクラスのオブジェクトを渡してください。

+0

ありがとう、しかし変数xは、このような関数を置くと値を変更しません! –

+0

x ++はx = x + 1と同じです –

+0

はい、それをインクリメントします。このコードはどこに置く必要がありますか? –

関連する問題