[Huawei Developers] Mike-mei:
Ads Kit offers Publisher Service for developers, helping them obtain high-quality ads by resource integration, based on a vast Huawei device user base; it also provides Identifier Service for advertisers to deliver personalized ads and attribute conversions.

Now, let's share some problems that developers often encounter when integrating the kit for your reference.

1. What can I do if the banner ad dimensions zoom in when the device screen orientation is switched from portrait to landscape?​

View attachment 5694581

Solution:

Fix the width or height of BannerView. For example, the height of the banner ad is fixed in the following code:

<com.huawei.hms.ads.banner.BannerView
android:id="@+id/hw_banner_view"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />

The following figure shows how a banner ad is normally displayed.

View attachment 5694583

2. How to request more than one native ads at a time?​

You can call loadAds() to request multiple native ads.

A request initiated by loadAds() should contain two parameters: AdParam and maxAdsNum. The maxAdsNum parameter specifies the maximum number (that is, 5) of ads to be loaded. The number of returned ads will be less than or equal to that requested, and the returned ones are all different. Sample code is as follows:

nativeAdLoader.loadAds(new AdParam.Builder().build(), 5);

After ads are successfully loaded, the SDK calls the onNativeAdLoaded() method of NativeAd.NativeAdLoadedListener multiple times to return a NativeAd object each time. After all the ads are returned, the SDK calls the onAdLoaded() method of AdListener to send a request success notification. If no ad is loaded, the SDK calls the onAdFailed() method of AdListener.

In the sample code below, testy63txaom86 is a test ad unit ID, which should be replaced with a formal one before app release.

NativeAdLoader.Builder builder = new NativeAdLoader.Builder(this, "testy63txaom86");
NativeAdLoader nativeAdLoader = builder.setNativeAdLoadedListener(new NativeAd.NativeAdLoadedListener() {
@Override
public void onNativeAdLoaded(NativeAd nativeAd) {
// Called each time an ad is successfully loaded.
...
}
}).setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Called when all the ads are successfully returned.
...
}
@Override
public void onAdFailed(int errorCode) {
// Called when all ads fail to be loaded.
...
}
}).build();
nativeAdLoader.loadAds(new AdParam.Builder().build(), 5);

Note: Before reusing NativeAdLoader to load ads, ensure that the previous request is complete.

3. What can I do if "onMediaError : -1" is reported when a roll ad is played in rolling mode in an app?​

After the roll ad is played for the first time, the error code -1 is returned when the roll ad is played again.

View attachment 5693969

Cause analysis:

1. The network is unavailable.

2. The roll ad is not released after the playback is complete. As a result, the error code -1 is returned during next playback.

Solution:

1. Check the network. To allow apps to use cleartext HTTP and HTTPS traffic on devices with targetSdkVersion 28 or later, configure the following information in the AndroidManifest.xml file:

<application
...
android:usesCleartextTraffic="true"
>
...
</application>

2. Release the roll ad in onMediaCompletion() of InstreamMediaStateListener. The roll ad needs to be released each time playback is complete.

public void onMediaCompletion(int playTime) {
updateCountDown(playTime);
removeInstream();
playVideo();
}
private void removeInstream() {
if (null != instreamView) {
instreamView.onClose();
instreamView.destroy();
instreamContainer.removeView(instreamView);
instreamContainer.setVisibility(View.GONE);
instreamAds.clear();
}
}

To learn more, please visit:

>> Ads Kit

>> Development Guide of Ads Kit