java - 如何为 admob 和加载创建单独的类并在 Activity 中显示?

标签 java android admob

我想为 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/

相关文章:

android - 更改布局后 admob 消失

java - JRadioButton 转换为 boolean 值?

java - 使用 apache CXF 使用 equals/hashCode 方法从 WSDL 生成 POJO

java - Android BroadcastReceiver 注册时出现空指针异常

android - 按Google Play服务版本过滤应用分发

java - 无法让 Admob 在多种布局上显示?

java - jPDFWriter库,将表格写入pdf文件

android - 如何在android xml中制作响应式布局?

android - Mosby MVP - 如何在 onDestroyView() 之后重新创建 Fragment 的状态?

ios - Google AdMob 不支持 CocoaPods