如何制作 Android 插页式广告?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11106322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create Android interstitial ads?
提问by Mohammad Abu Hmead
I tried things from many blogs but none gave a step-by-step solution. Should I edit something on the AdMob site? I created the site from the ad sit/app option under the Sites & Apps tab.
我尝试了许多博客中的内容,但没有一个给出分步解决方案。我应该在 AdMob 网站上编辑某些内容吗?我从“站点和应用程序”选项卡下的“广告位置/应用程序”选项创建了该站点。
I used this code:
我使用了这个代码:
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);
回答by Mika?l Mayer
Using the last Android framework, I figured out that I need to call the load() function each time the ad is closed.
使用最后一个 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);
}
}
回答by Eric Leichtenschlag
Unlike banners, insterstitial ads don't automatically show once their loaded. You'll have to listen for AdMob's onReceiveAd()
callback, and inside that callback, call interstital.show()
to show your interstitial.
与横幅不同,插页式广告在加载后不会自动显示。您必须监听 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();
}
}
}
Check out a code example here. This example will show the interstitial as soon as it is received. Alternatively, you may want to wait until a proper time to show the interstitial, such as at the end of a game level, and you could check interstitial.isReady()
to see if you can show the interstitial.
在此处查看代码示例。此示例将在收到插页式广告后立即显示。或者,您可能希望等到适当的时间显示插页式广告,例如在游戏关卡结束时,您可以检查interstitial.isReady()
是否可以显示插页式广告。
回答by LiranNis
You cant implement AdListener anymore, I used it this way:
你不能再实现 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);
Place your own ID in strings.xml named interstitial_ad_unit_id, or replace
将您自己的 ID 放在名为 interstitial_ad_unit_id 的 strings.xml 中,或替换
getResources().getString(R.string.interstitial_ad_unit_id)
with your ID.
用你的身。
回答by user2490225
I think this example code will help you out.
我认为这个示例代码会帮助你。
How to Add AdMob Interstitial Ads in Your Android Apps
如何在您的 Android 应用中添加 AdMob 插页式广告
In this example, it shows you the whole source code. And it also provides some solutions for common errors. For example, onFailedToReceiveAd error solution and no ad returned solution. You can also download the source code from there.
在这个例子中,它向你展示了整个源代码。并且还提供了一些常见错误的解决方案。例如,onFailedToReceiveAd 错误解决方案和无广告返回解决方案。您也可以从那里下载源代码。
回答by Shygar
FYI, the interstitial.isReady()
method is no longer supported. This is the correct method:
仅供参考,该interstitial.isReady()
方法不再受支持。这是正确的方法:
if (interstitial.isLoaded()) {
interstitial.show();
}
回答by Aykut Uluda?
Call this function on app's opening:
在应用程序打开时调用此函数:
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);
}
Then you can show ads:
然后你可以展示广告:
if (interstitial.isLoaded()){
interstitial.show();
}
You should prepare ads before showing it. These took 3-5 seconds depending internet speed of device.
您应该在展示之前准备好广告。这些需要 3-5 秒,具体取决于设备的互联网速度。
回答by Adeeb karim
adding banner and interstitial ads:
添加横幅和插页式广告:
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);
}
and in XML:
并在 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>
回答by passionatedevops
Try this in MainActivity.java
在 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();
}
}
回答by Rahul Singh
InterstitialAd mInterstitialAd;
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.");
}