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 或者直接掃碼

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM