admob广告代码参考 安卓 banner广告

文章目录

各行业谷歌相关交流联系微信:toushouapp-com ,认证可进群一起探讨问题.

Add AdView to the layout

The first step toward displaying a banner is to place AdView in the layout for the Activity or Fragment in which you'd like to display it. The easiest way to do this is to add one to the corresponding XML layout file. Here's an example that shows AdView at the bottom of an Activity:

项目交流联系微信:toushouapp-com,或验证加交流QQ群:595990939,添加需告知来意。

main_activity.xml

 

微信公众号
关注公众号“投手网”,查看隐藏内容

使用微信扫描二维码关注公众号后,回复“查看码”获取查看码

JAVA

KOTLIN

 

 

 

AdView adView = new AdView(this);
adView
.setAdSize(AdSize.BANNER);
adView
.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// TODO: Add adView to your view hierarchy.

Warning:
 Make sure you set the ad size and ad unit ID in the same manner (i.e. set both in XML or both programmatically).

Always test with test ads

When building and testing your apps, make sure you use test ads rather than live, production ads. Failure to do so can lead to suspension of your account.

The easiest way to load test ads is to use our dedicated test ad unit ID for Android banners:

ca-app-pub-3940256099942544/5224354917

It's been specially configured to return test ads for every request, and you're free to use it in your own apps while coding, testing, and debugging. Just make sure you replace it with your own ad unit ID before publishing your app.

For more information about how the Mobile Ads SDK's test ads work, see Test Ads.

Load an ad

Note:
 Make all calls to the Mobile Ads SDK on the main thread.

Once the AdView is in place, the next step is to load an ad. That's done with the loadAd()method in the AdView class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.

Here's an example that shows how to load an ad in the onCreate() method of an Activity:

MainActivity (excerpt)

JAVA

KOTLIN

 

 

 

package ...

import ...
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
private AdView mAdView;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView
(R.layout.activity_main);

MobileAds.initialize(this,
"ca-app-pub-3940256099942544~3347511713");

mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView
.loadAd(adRequest);
}
}

That's it! Your app is now ready to display banner ads.

Ad events

To further customize the behavior of your ad, you can hook onto a number of events in the ad's lifecycle: loading, opening, closing, and so on. You can listen for these events through theAdListener class.

To use an AdListener with AdView, simply call the setAdListener() method:

JAVA

KOTLIN

 

 

 

mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}

@Override
public void onAdFailedToLoad(int errorCode) {
// Code to be executed when an ad request fails.
}

@Override
public void onAdOpened() {
// Code to be executed when an ad opens an overlay that
// covers the screen.
}

@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}

@Override
public void onAdClosed() {
// Code to be executed when when the user is about to return
// to the app after tapping on an ad.
}
});

Each of the overridable methods in AdListener corresponds to an event in the lifecycle of an ad.

Overridable methods
onAdLoaded() The onAdLoaded() method is executed when an ad has finished loading. If you want to delay adding the AdView to your activity or fragment until you're sure an ad will be loaded, for example, you can do so here. If you're using a third-party analytics package to track impressions, this is also where you can place the call to record them.
onAdFailedToLoad() The onAdFailedToLoad() method is the only one that includes a parameter. The errorCode parameter indicates what type of failure occurred. The possible values are defined as constants in the AdRequestclass:

  • ERROR_CODE_INTERNAL_ERROR - Something happened internally; for instance, an invalid response was received from the ad server.
  • ERROR_CODE_INVALID_REQUEST - The ad request was invalid; for instance, the ad unit ID was incorrect.
  • ERROR_CODE_NETWORK_ERROR - The ad request was unsuccessful due to network connectivity.
  • ERROR_CODE_NO_FILL - The ad request was successful, but no ad was returned due to lack of ad inventory.
onAdOpened() This method is invoked when the user taps on an ad. If you're using an analytics package to track clickthroughs, this is a good place to record one.
onAdLeftApplication() This method is invoked after onAdOpened(), when a user click opens another app (such as the Google Play), backgrounding the current app.
onAdClosed() When a user returns to the app after viewing an ad's destination URL, this method is invoked. Your app can use it to resume suspended activities or perform any other work necessary to make itself ready for interaction. See the AdMob AdListener example for an implementation of the ad listener methods in the Android API Demo app.

Banner sizes

Note: The size of the container in which you place your ad must be at least as big as the banner. If your container has padding, that effectively decreases the size of your container. In the event that the container cannot fit the banner ad, the banner will not appear, and you will get this warning in the logs:

 

 

W/Ads: Not enough space to show ad. Needs 320x50 dp, but only has 288x495 dp.

The table below lists the supported banner sizes.

Size in dp (WxH) Description Availability AdSize constant
320x50 Standard Banner Phones and Tablets BANNER
320x100 Large Banner Phones and Tablets LARGE_BANNER
300x250 IAB Medium Rectangle Phones and Tablets MEDIUM_RECTANGLE
468x60 IAB Full-Size Banner Tablets FULL_BANNER
728x90 IAB Leaderboard Tablets LEADERBOARD
Screen width x 32|50|90 Smart Banner Phones and Tablets SMART_BANNER

Note:
 If an app tries to load a banner that's too big for its layout, the SDK won't display it. Instead, an error message will be written to the log.

Smart Banners

Smart Banners are ad units that render screen-width banner ads on any screen size across different devices in either orientation. Smart Banners help deal with increasing screen fragmentation across different devices by "smartly" detecting the width of the device in its current orientation and making the ad view that size.

Three ad heights are implemented in smart banners:

Ad height Screen height
32 dp ≤ 400 dp
50 dp > 400 dp and ≤ 720 dp
90 dp > 720 dp

Typically, Smart Banners on phones have a height of 50 dp in portrait and 32 dp in landscape. On tablets, height is normally 90 dp in both orientations.

When an image ad isn't large enough to take up the entire allotted space, the image will be centered, and the space on either side will be filled in.

To use Smart Banners in XML, specify the constant SMART_BANNER for the ad size and set the width of the AdView to match_parent. For example:

 

 

 

<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

To create a Smart Banner programmatically, use AdSize.SMART_BANNER as the ad size:

JAVA

KOTLIN

 

 

 

AdView adView = new AdView(this);
adView
.setAdSize(AdSize.SMART_BANNER);

Additional resources

Samples on GitHub

  • Minimal implementation of banner ads example: Java | Kotlin

  • Advanced features demo: Java | Kotlin

Mobile Ads Garage video tutorials

  • Banner Implementation
  • Banner Best Practices

Next steps

  • If you haven't already, create your own app and banner ad unit in the AdMob UI and use your newly created app ID and ad unit ID in your code. Remember to configure your device with test ads.
  • Learn about ad targeting and banner ad guidance.
  • Try another ad format:
    • Interstitial
    • Rewarded Video
    • Native

关注微信公众号

微信公众号
toushouappcom

扫码入群

微信扫一扫
长按识别入群

QQ扫描加群

QQ扫描加群
595990939


投手网-优化师-教程 » admob广告代码参考 安卓 banner广告

扫码,关注更多行业内容
微信二维码
APP产品投放无服务费
QQ二维码
微信号:
QQ号:
复制文字

toushouapp-com

复制文字

2109763290