如何创建Android插屏广告?

12

我尝试了许多博客上的方法,但没有一篇给出了逐步解决方案。我应该在AdMob网站上进行编辑吗?我是在“网站和应用程序”选项卡下的广告站/应用程序选项中创建的该站点。

我使用了这段代码:

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);

假设“MyAdMobId”已被替换为您的实际ID,并且您正在模拟器中进行测试,这应该可以工作。您在日志中看到了什么?(请注意,AdMob要求您被列入白名单才能提供网络插页式广告) - Eric Leichtenschlag
日志中没有任何信息,但我如何知道是否已被列入白名单以提供网络插页广告? - Mohammad Abu Hmead
你可能没有被列入白名单。AdMob仅向少数人提供网络插页式广告(请参见此处)。除非你被列入白名单,否则你只能获取测试插页式广告,或者如果你设置了插页式广告活动,则可以获得一个内部广告插页。 - Eric Leichtenschlag
9个回答

13

使用最新的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);
  }
}

感谢在 onAdClosed 中调用 loadAd。这样你就可以准备显示另一个广告了。 - Jared Burrows
你目前正在使用哪个库? - suresh madaparthi
抱歉,我不再在Android上进行编程了。 - Mikaël Mayer

11

与横幅广告不同,插屏广告在加载完成后不会自动显示。你需要监听 AdMob 的 onReceiveAd() 回调,在该回调中调用 interstitial.show() 来展示你的插屏广告。

public YourActivity extends Activity implements AdListener {
  ...

  @Override
  public void onReceiveAd(Ad ad) {
    Log.d("OK", "Received ad");
    if (ad == interstitial) {
      interstitial.show();
    }
  }
}

在这里查看一个代码示例该示例将尽快显示插页式广告。或者,您可能希望在适当的时间显示插页式广告,例如在游戏级别结束时,并且可以使用interstitial.isReady()检查是否可以显示插页式广告。


6

您不能再实现AdListener了,我是这样使用的:

final InterstitialAd mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id));
AdRequest adRequestInter = new AdRequest.Builder().build();
mInterstitialAd.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
        mInterstitialAd.show();
    }
});
mInterstitialAd.loadAd(adRequestInter);

在strings.xml中命名为interstitial_ad_unit_id并放置您自己的ID,或者替换掉原有的ID。
getResources().getString(R.string.interstitial_ad_unit_id)

使用您的ID。


1
显示这样的广告是非常糟糕的,你可能会被 Google 封锁。最好在 onAdLoaded() 回调之外的其他地方使用 if(mInterstitialAd.isLoaded()) mInterstitialAd.show();,以避免意外点击。请参考:https://support.google.com/admob/answer/6213019?hl=en - hiddeneyes02

3

我认为这个示例代码会对你有所帮助。

如何在Android应用中添加AdMob插页式广告

在这个示例中,它展示了整个源代码。它还提供了一些常见错误的解决方案。例如,onFailedToReceiveAd错误解决方案和没有广告返回的解决方案。你也可以从那里下载源代码。


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秒,具体时间取决于设备的网络速度。

1

了解一下,interstitial.isReady() 方法已不再支持。以下是正确的方法:

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

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>

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(); 
        } 
    } 

0

插页式广告 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.");
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接