我有一个没有非页内广告的源代码。我正在尝试添加它,但它显示错误Cannot resolve method showAdInter()
这是片段
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.AdRequest;
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitialAd;
boolean exitApp = false;
private void launchInter()
{
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId("ca-app-pub-4164100745099699/5685474562");
//set the adListener
interstitialAd.setAdListener(new AdListener(){
@Override
public void onAdListener() {
showAdInter();
}
@Override
public void onAdFailedToLoad(int errorCode) {
}
@Override
public void onAdClosed() {
if (exitApp)
finish();
}
});
}
}
答案 0 :(得分:0)
MobileAds.initialize(this, "ca-app-pub-2404474835802386~9054675954");
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(ca-app-pub-2404474835802386/3008142357);
为加载广告添加此方法:
public void startGame() {
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
if (!mInterstitialAd.isLoading() && !mInterstitialAd.isLoaded()) {
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}
现在您只需在展示广告时调用此方法。
public void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
System.out.println("add is not loaded");
}
}