gpt4 book ai didi

android - 在没有按下按钮的情况下添加插页式 admob 广告

转载 作者:行者123 更新时间:2023-11-29 19:21:16 25 4
gpt4 key购买 nike

我想通过 admob 添加插页式广告而不按任何按钮但是
它没有出现你能帮我试过下面的代码吗:

...
public class MainActivity extends ActionBarActivity {

InterstitialAd mInterstitialAd;
Button mNewGameButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});

requestNewInterstitial();

mNewGameButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}
}
});

beginPlayingGame();
}

private void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEVICE_ID")
.build();

mInterstitialAd.loadAd(adRequest);
}

private void beginPlayingGame() {
// Play for a while, then display the New Game Button
}
}
...

最佳答案

加载添加时,您必须添加一个用于测试的设备 ID。当您运行此代码时,请查看您的 logcat 以查看设备 ID .. 将该设备 ID 复制并粘贴到添加测试设备中,然后重新运行您将开始看到添加

公共(public)类 MainActivity 扩展了 ActionBarActivity {

InterstitialAd mInterstitialAd;
Button mNewGameButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");

AdRequest adRequest = new AdRequest.Builder() .addTestDevice("SEE_YOUR_LOGCAT_TO_GET_YOUR_DEViCE_ID").build();

    mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
requestNewInterstitial();
beginPlayingGame();
}
});

requestNewInterstitial();

if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
beginPlayingGame();
}


beginPlayingGame();
}

关于android - 在没有按下按钮的情况下添加插页式 admob 广告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42422745/

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