我尝试过很多博客,但都没有提供逐步的解决方案。我应该在AdMob网站上编辑某些内容吗?我是通过网站& amp;中的广告位/应用选项创建的。应用标签。
我使用了这段代码:
interstitial = new InterstitialAd(this, "MyAdMobID");
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(this);
// Create ad request
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(AdRequest.TEST_EMULATOR);
// Begin loading your interstitial
interstitial.loadAd(adRequest);
adRequest.setTesting(true);
答案 0 :(得分:12)
使用最后一个Android框架,我发现每次广告关闭时我都需要调用load()函数。
import com.google.android.gms.ads.*;
import android.os.Handler;
import android.os.Looper;
import android.app.Activity;
class MyActivity extends Activity implements AdListener {
private InterstitialAd adView; // The ad
private Handler mHandler; // Handler to display the ad on the UI thread
private Runnable displayAd; // Code to execute to perform this operation
@Override
public void onCreate(Bundle savedInstanceState) {
adView = new InterstitialAd(mContext);
adView.setAdUnitId("ca-app-pub-XXXXXXXXXX");
adView.setAdListener(this);
mHandler = new Handler(Looper.getMainLooper());
displayAd = new Runnable() {
public void run() {
runOnUiThread(new Runnable() {
public void run() {
if (adView.isLoaded()) {
adView.show();
}
}
});
}
};
loadAd();
}
@Override
public void onAdClosed() {
loadAd(); // Need to reload the Ad when it is closed.
}
void loadAd() {
AdRequest adRequest = new AdRequest.Builder()
//.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.build();
// Load the adView object witht he request
adView.loadAd(adRequest);
}
//Call displayInterstitial() once you are ready to display the ad.
public void displayInterstitial() {
mHandler.postDelayed(displayAd, 1);
}
}
答案 1 :(得分:11)
与横幅广告不同,插页式广告一旦加载就不会自动展示。您必须收听AdMob的onReceiveAd()
回调,并在该回调中,请致电interstital.show()
以显示您的插页式广告。
public YourActivity extends Activity implements AdListener {
...
@Override
public void onReceiveAd(Ad ad) {
Log.d("OK", "Received ad");
if (ad == interstitial) {
interstitial.show();
}
}
}
查看代码示例here。此示例将在收到插页式广告后立即显示插页式广告。或者,您可能希望等到适当的时间来显示插页式广告,例如在游戏级别结束时,您可以查看interstitial.isReady()
以查看是否可以显示插页式广告。
答案 2 :(得分:3)
我认为这个示例代码可以帮助您。
How to Add AdMob Interstitial Ads in Your Android Apps
在此示例中,它显示了整个源代码。它还为常见错误提供了一些解决方案。例如,onFailedToReceiveAd错误解决方案并且没有广告返回解决方案。您也可以从那里下载源代码。
答案 3 :(得分:1)
仅供参考,不再支持interstitial.isReady()
方法。这是正确的方法:
if (interstitial.isLoaded()) {
interstitial.show();
}
答案 4 :(得分:1)
在应用开场时调用此功能:
InterstitialAd interstitial;
public void AdMob() {
AdRequest adRequest = new AdRequest.Builder().addTestDevice("YOUR_TEST_DEVICE_ID").build();
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("YOUR_AD_ID");
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
//Ads loaded
}
@Override
public void onAdClosed() {
super.onAdClosed();
//Ads closed
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
//Ads couldn't loaded
}
});
interstitial.loadAd(adRequest);
}
然后你可以展示广告:
if (interstitial.isLoaded()){
interstitial.show();
}
您应该在展示之前准备广告。根据设备的互联网速度,这需要3-5秒。
答案 5 :(得分:0)
添加横幅广告和非页内广告:
AdView mAdView;
InterstitialAd interstitialAd;
ProgressDialog pd;
void showAds(){
if(interstitialAd.isLoaded()){
interstitialAd.show();
}
}
public void initAds(){
mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
interstitialAd=new InterstitialAd(this);
interstitialAd.setAdUnitId(getResources().getString(R.string.inetial3));
AdRequest adRequest1=new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest1);
}
和XML:
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id2"
android:layout_gravity="bottom|center">
</com.google.android.gms.ads.AdView>
答案 6 :(得分:0)
在MainActivity.java中尝试
private InterstitialAd interstitial;
// Initialize the Mobile Ads SDK
MobileAds.initialize(this, getString(R.string.admob_app_id));
AdRequest adIRequest = new AdRequest.Builder().build();
// Prepare the Interstitial Ad Activity
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
//add admob_interstitial_id unit id in string file
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
// Interstitial Ad load Request
interstitial.loadAd(adIRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener()
{
public void onAdLoaded()
{
// Call displayInterstitial() function when the Ad loads
displayInterstitial();
}
});
}
public void displayInterstitial()
{
// If Interstitial Ads are loaded then show else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}
答案 7 :(得分:0)
InterstitialAd mInterstitialAd;
//To load InterstitialAd ads
//app_id for test ca-app-pub-3940256099942544~3347511713
//full_id for test ca-app-pub-3940256099942544/1033173712
MobileAds.initialize(this, getString(R.string.app_id));
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.full_id));
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Load the next interstitial
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
});
//To show ads
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}