• Google Admob移动广告快速集成步骤


    Google Admob移动广告快速集成步骤

    第一步:引入依赖包
    //admob广告
    implementation 'com.google.android.gms:play-services-ads:17.2.0'
    第二步:在清单文件中设置appID
    <application
    <!-- admob配置 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            <!-- 注意 这里设置应用id 而不是广告单元id 每个广告都有各自独立的id -->
            android:value="ca-app-pub-xxxxxxxxxxxxxxxxxxxx"/>
    </application>
    第三步:在布局文件中设置广告显示的具体位置
    <!-- 布局中可以设置广告单元id  这里考虑到防止反编译 改成在代码中设置-->
    <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="SMART_BANNER"
        ></com.google.android.gms.ads.AdView>
    第四步:初始化Admob
    // 初始化Admob  这个地方填appid 注意
    MobileAds.initialize(this, "ca-app-pub-xxxxxxxxxxxxxxxxxxxx");
    第五步: 在对应的Activity或Fragment中设置广告显示
    private static final String AD_UNIT_ID = "ca-app-pub-xxxxxxxxxxxxxxxxxxxx";
    
    private void initAdmob() {
        mAdView = findViewById(R.id.adView);
        mAdView.setAdUnitId(AD_UNIT_ID);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                // Code to be executed when an ad finishes loading.
                //广告加载完成后,系统会执行 onAdLoaded() 方法。
                // 如果您想延迟向 Activity 或 Fragment 中添加AdView的操作(例如,延迟到您确定广告会加载时),可以在此处进行。
            }
    
            @Override
            public void onAdFailedToLoad(int errorCode) {
                // Code to be executed when an ad request fails.
                //onAdFailedToLoad() 是唯一包含参数的方法。errorCode 参数会指明发生了何种类型的失败。系统将这些可能的类型值定义为AdRequest类中的如下常量:
                //ERROR_CODE_INTERNAL_ERROR - 内部出现问题;例如,收到广告服务器的无效响应。
                //ERROR_CODE_INVALID_REQUEST - 广告请求无效;例如,广告单元 ID 不正确。
                //ERROR_CODE_NETWORK_ERROR - 由于网络连接问题,广告请求失败。
                //ERROR_CODE_NO_FILL - 广告请求成功,但由于缺少广告资源,未返回广告。
            }
    
            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
                //此方法会在用户点按广告时调用。
            }
    
            @Override
            public void onAdClicked() {
                // Code to be executed when the user clicks on an ad.
            }
    
            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
                //此方法会于 onAdOpened() 之后在用户点击打开其他应用(例如,Google Play)时调用,从而在后台运行当前应用。
            }
    
            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
                //在用户查看广告的目标网址后返回应用时,会调用此方法。应用可以使用此方法恢复暂停的活动,或执行任何其他必要的操作,以做好互动准备。
                // 有关 Android API Demo 应用中广告监听器方法的实现方式,请参阅 AdMob AdListener 示例。
            }
        });
    }

    关于我

    私人博客

    技术微信公众号:infree6 或者直接扫码

     

     
  • 相关阅读:
    APDU:APDU常用指令
    其他:IntelliJ IDEA设置运行内存
    Linux:获取Linux离线postgresql数据库安装包并部署
    资源:Postgresql数据库下载路径
    免费版:Xshell和Xftp下载路径
    Raspberry Pi:树莓派开发板配置USB启动系统
    Raspberry Pi:树莓派安装基础系统 Raspberry Pi(树莓派系统)
    Raspberry Pi:树莓派安装Kali2021新版本
    Docker:虚拟机挂起后,再启动docker容器连接失败
    Docker:docker搭建redis一主多从集群(配置哨兵模式)
  • 原文地址:https://www.cnblogs.com/songjianzaina/p/11286953.html
Copyright © 2020-2023  润新知