2016-11-08 100 views
0

我想知道如何在每次按下按鈕時顯示Admob插頁式廣告。我設法顯示插頁式廣告,但僅在第一次按下按鈕時才顯示。這裏是我的代碼:如何在每次單擊按鈕時顯示Admob插頁式廣告

package com.mycash.borgjake.mycash; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

import android.widget.Button; 
import android.widget.TextView; 

import android.view.View; 

import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 
import com.google.android.gms.ads.InterstitialAd; 

import static com.mycash.borgjake.mycash.R.styleable.View; 

public class MainActivity extends AppCompatActivity { 

private InterstitialAd mInterstitial; 


Button btnClick; 
Button btnWithdraw; 

TextView txtBalance; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    AdView adView = (AdView)findViewById(R.id.adView); 
    AdRequest adRequest = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
    adView.loadAd(adRequest); 

    btnClick = (Button) findViewById(R.id.button); 

    txtBalance = (TextView) findViewById(R.id.textView); 

    btnClick.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      String countValue = txtBalance.getText().toString(); 
      int dblCountValue = Integer.parseInt(countValue); 

      if(mInterstitial.isLoaded()) { 
       mInterstitial.show(); 
      } 

      dblCountValue++; 
      txtBalance.setText(String.valueOf(dblCountValue)); 


     } 
    }); 

    mInterstitial = new InterstitialAd(this); 
    mInterstitial.setAdUnitId("..."); 
    AdRequest request = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
    mInterstitial.loadAd(request); 

} 

public void onButtonClick(View v) { 
    if(v.getId() == R.id.button2) { 
     Intent i = new Intent(MainActivity.this, Withdraw.class); 
     startActivity(i); 
    } 
    } 
} 

感謝提前:)

+2

呼叫mInterstitial.loadAd(要求);用戶關閉Interstitial添加後。 –

+0

我該如何去做呢? – user3704212

+0

mInterstitialAd.setAdListener(新AdListener的(){ @Override 公共無效onAdClosed(){// 繼續執行代碼 } 。}); –

回答

1

幫助添加在按鈕下面一行首先的onClick()方法,

AdRequest request = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .build(); 
mInterstitial.loadAd(request); 
1

加載廣告,每當用戶點擊所以你需要在裏面寫你的代碼btnClick.setOnClickListener

btnClick.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     AdRequest request = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .build(); 
     mInterstitial.loadAd(request); 
    } 
}); 
+0

我是android開發的初學者。我很抱歉,但我不理解。 – user3704212

0

你需要撥打

mInterstitial.loadAd(request); 

在您的按鈕顯示它之後單擊偵聽器。

最終代碼;

package com.mycash.borgjake.mycash; 

import android.content.Intent; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

import android.widget.Button; 
import android.widget.TextView; 

import android.view.View; 

import com.google.android.gms.ads.AdListener; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdView; 
import com.google.android.gms.ads.InterstitialAd; 

import static com.mycash.borgjake.mycash.R.styleable.View; 

public class MainActivity extends AppCompatActivity { 

private InterstitialAd mInterstitial; 


Button btnClick; 
Button btnWithdraw; 

TextView txtBalance; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 

mInterstitial = new InterstitialAd(this); 

AdView adView = (AdView)findViewById(R.id.adView); 
AdRequest adRequest = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .build(); 
adView.loadAd(adRequest); 

btnClick = (Button) findViewById(R.id.button); 

txtBalance = (TextView) findViewById(R.id.textView); 

btnClick.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     String countValue = txtBalance.getText().toString(); 
     int dblCountValue = Integer.parseInt(countValue); 

     if(mInterstitial.isLoaded()) { 
      mInterstitial.show(); 
     } 
     AdRequest request = new AdRequest.Builder() 
      .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
      .build(); 
     mInterstitial.loadAd(request); 

     dblCountValue++; 
     txtBalance.setText(String.valueOf(dblCountValue)); 


    } 
}); 

mInterstitial.setAdUnitId("..."); 
AdRequest request = new AdRequest.Builder() 
     .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) 
     .build(); 
mInterstitial.loadAd(request); 

} 

public void onButtonClick(View v) { 
if(v.getId() == R.id.button2) { 
    Intent i = new Intent(MainActivity.this, Withdraw.class); 
    startActivity(i); 
    } 
    } 
} 

只需複製&粘貼編輯的代碼。 :)然後閱讀下面的解釋。

+0

我收到一個錯誤。在代碼行:mInterestitial.loadAd(request); – user3704212

+0

我修正了上面的代碼。嘗試重新複製並粘貼它。 – dcanbatman

1

試試這個代碼..

首先,你需要declaire您在InterstitialAd並在AdRequest變量作爲全球類似..

private InterstitialAd mInterstitialAd; 
private AdRequest adRequest; 

然後在您的活動onCreate方法添加此代碼..

adRequest = new AdRequest.Builder().addTestDevice("PUT YOUR DEVICE TEST ID HERE").build(); 

然後在按鈕單擊中寫下此代碼..

mInterstitialAd = new InterstitialAd(getApplicationContext()); 
       mInterstitialAd.setAdUnitId("ADD YOUR ADUNITID HERE"); 
       mInterstitialAd.loadAd(adRequest); 
       mInterstitialAd.setAdListener(new AdListener() { 
        public void onAdLoaded() { 
         showInterstitial(); 
        } 
       }); 

最後,在您的活動類添加這個方法並運行你的項目..

private void showInterstitial() { 
    if (mInterstitialAd.isLoaded()) { 
     mInterstitialAd.show(); 
    } 
} 

我希望這將工作...

相關問題