Google Admob Mobile Ads rapid integration step

Google Admob Mobile Ads rapid integration step

The first step: introducing dependencies
//admob广告
implementation 'com.google.android.gms:play-services-ads:17.2.0'

 

Step Two: Set in the manifest file appID
< File application
 <- AdMob Configuration ->! 
    <Meta- Data 
        Android: name = "com.google.android.gms.ads.APPLICATION_ID" 
        <- Note that instead of setting the application id id ad units each ad! have a separate ID -> 
        Android: value = "CA-App-Pub-xxxxxxxxxxxxxxxxxxxx" /> 
</ file application>

 

Step 3: Set the specific location of the ad displayed in the layout file
<! - Layout id ad units may be provided here in view of preventing change in the decompiled code setting -> 
<com.google.android.gms.ads.AdView xmlns: ADS = "http://schemas.android .com / APK / RES-Auto " 
    Android: ID =" @ + ID / adView " 
    Android: layout_width =" the wrap_content " 
    Android: layout_height =" the wrap_content " 
    Android: layout_alignParentBottom =" to true " 
    Android: layout_centerHorizontal =" to true " 
    ADS: adSize = "SMART_BANNER" 
    > </ com.google.android.gms.ads.AdView>

 

Step four: initialization Admob
// initialize Admob fill this place appid attention 
MobileAds.initialize ( the this , "CA-App-Pub-xxxxxxxxxxxxxxxxxxxx");

 

Fifth step: the provided advertisement display or corresponding Fragment Activity in
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() 方法。
            //If you want to add delay to the operation AdView Activity or Fragment (for example, the delay to determine when your ads are loaded), can be carried out here. 
        } 

        @Override 
        public  void onAdFailedToLoad ( int errorCode) {
             // Code to BE AN AD Executed When Request fails Total.
             // onAdFailedToLoad () is the only method comprising parameters. errorCode parameter will indicate what type of failure occurred. These types of systems may AdRequest class is defined as the following constants:
             // ERROR_CODE_INTERNAL_ERROR - internal problems; e.g., the ad server received an invalid response.
            // ERROR_CODE_INVALID_REQUEST - ad request is invalid; e.g., the ad unit ID is incorrect.
            // ERROR_CODE_NETWORK_ERROR - because of network connectivity issues, the ad request fails.
            // ERROR_CODE_NO_FILL - ad request was successful, but because of the lack of advertising resources, did not return an ad. 
        }

        @Override 
        public  void onAdOpened () {
             // Code to the when the Executed BE AN AD that overlay the Opens AN
             // Covers at The Screen.
             // This method is invoked by the user when the ad point. 
        } 

        @Override 
        public  void onAdClicked () {
             // Code The User to BE Executed When Clicks ON AN AD. 
        } 

        @Override 
        public  void onAdLeftApplication in () {
             // Code to BE The Executed When User has left The App.
             // This method after calling time will be in onAdOpened () click to open other applications (for example, Google Play) in user, so the current application running in the background.
        } 

        @Override 
        public  void onAdClosed () {
             // Code to BE the when the Executed at The return to the About the User IS
             // to the After tapping at The App ON AN AD.
             // When the user views the ad's destination URL returns to the application, calls this method. Applications can use this method to resume suspended activities or perform any other necessary operations to ready for interaction.
            // implementation dependent Android API Demo applications ads listener method, see AdMob AdListener example. 
        } 
    }); 
}

 

about me

Private blog

Public art micro-channel Number: infree6 either directly scan code

 

 

Guess you like

Origin www.cnblogs.com/songjianzaina/p/11286953.html