我想为 AdMob 创建一个单独的 java 类,如何在 android java 中加载和显示不活动状态。
最佳答案
我想您正在寻找的是有关如何从中心角度管理应用程序中的广告的一些想法。下面是一个示例代码,您可以尝试使用它来创建它们并在一个地方控制它们。也可以创建类似的函数来管理其他类型。
/**
* ad manager class managing the ads initializing and showing them
*/
class AdManager(context: Context){
init(){
MobileAds.initialize(context, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
//do something on initialization or leave it as it is
}
});
}
/**
* this function controls showing the xml banner ads
*/
fun showAd(adView: AdView){
//some add showing code goes here
val adRequest = AdRequest.Builder().build()
adView.loadAd(adRequest)
}
fun listenForChanges(context: Context, adview: AdView){
adview.adListener = object: AdListener() {
override fun onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
override fun onAdFailedToLoad(errorCode : Int) {
// Code to be executed when an ad request fails.
}
override fun onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}
override fun onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
override fun onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
override fun onAdClosed() {
// Code to be executed when the user is about to return
// to the app after tapping on an ad.
}
}
//similar functions can be created and used for other type of ads
}
关于java - 如何为 admob 和加载创建单独的类并在 Activity 中显示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59645470/