gpt4 book ai didi

android - 如何制作 Android 插页式广告?

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:50:51 29 4
gpt4 key购买 nike

我尝试了很多博客中的东西,但没有一个给出了逐步解决方案。我应该在 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);

最佳答案

使用上一个 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);
}
}

关于android - 如何制作 Android 插页式广告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11106322/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com