在Android中,Google AdMob的InterstitialAd已经过时,应该使用新的全屏广告(FullScreen Ads)来代替。以下是一个使用新的全屏广告的示例代码:
首先,确保已经导入最新版本的Google AdMob库。在build.gradle文件中添加以下依赖项:
implementation 'com.google.android.gms:play-services-ads:19.8.0'
然后,创建一个新的InterstitialAd对象,并加载广告:
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
private InterstitialAd mInterstitialAd;
private Button mShowAdButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mShowAdButton = findViewById(R.id.show_ad_button);
// 创建InterstitialAd实例
InterstitialAd.load(this, "ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(InterstitialAd interstitialAd) {
// 广告加载成功
mInterstitialAd = interstitialAd;
// 设置广告显示的回调
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdDismissedFullScreenContent() {
// 广告已经关闭
Log.d("TAG", "Ad was dismissed.");
// 在这里可以加载下一个广告
loadAd();
}
@Override
public void onAdFailedToShowFullScreenContent(com.google.android.gms.ads.AdError adError) {
// 广告展示失败
Log.d("TAG", "Ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// 广告展示成功
Log.d("TAG", "Ad showed fullscreen content.");
// 清空InterstitialAd对象,以便下一次加载新的广告
mInterstitialAd = null;
}
});
// 广告加载成功后,显示按钮
mShowAdButton.setVisibility(View.VISIBLE);
}
@Override
public void onAdFailedToLoad(com.google.android.gms.ads.LoadAdError loadAdError) {
// 广告加载失败
Log.d("TAG", loadAdError.getMessage());
mInterstitialAd = null;
}
});
mShowAdButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 显示广告
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
}
}
});
}
private void loadAd() {
// 加载新的广告
InterstitialAd.load(this, "ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx", new AdRequest.Builder().build(), new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(InterstitialAd interstitialAd) {
// 广告加载成功
mInterstitialAd = interstitialAd;
}
@Override
public void onAdFailedToLoad(com.google.android.gms.ads.LoadAdError loadAdError) {
// 广告加载失败
Log.d("TAG", loadAdError.getMessage());
mInterstitialAd = null;
}
});
}
}
在上面的代码中,我们使用InterstitialAd.load()
方法来加载广告,并设置广告的显示和关闭的回调。当广告加载成功后,我们可以调用InterstitialAd.show()
方法来显示广告。当广告显示完毕后,会触发相应的回调方法进行处理。
请确保将ca-app-pub-xxxxxxxxxxxxxxx/xxxxxxxxxx
替换为你的广告单元ID。另外,还需要在AndroidManifest.xml文件中添加适当的权限和相关配置,以便使用Google AdMob广告服务。
需要注意的是,全屏广告需要一定的加载时间,在显示广告前,我们应该在后台加载下一个广告,以便在当前广告关闭后能够立即