HMS-Core / hms-ads-demo-java

HUAWEI Ads SDK sample code. HUAWEI Ads SDK provides the banner, native, rewarded, interstitial and splash ad formats for integration.
https://developer.huawei.com/consumer/en/doc/development/HMS-Guides/ads-sdk-introduction?ha_source=hms1
Apache License 2.0
57 stars 20 forks source link

Banner ad doesn't work in android library #23

Open nboy1 opened 11 months ago

nboy1 commented 11 months ago

i'm trying to create a android library from huawei ads sdk i've tried everything i can imaging but banner doesn't show up. note: i'm testing on nox player and your example apk work fine on this nox player, this my code:

public class AdaptiveBanner { private FREContext context; private FrameLayout frameLayout; private BannerView hwBannerView; boolean initialLayoutComplete = false; int height; int total; int ColorValue;TextView textView; public void setContext(FREContext ctx) { this.context = ctx; } public void showAdaptiveBanner(String bannerKey, String position) {

    hwBannerView = new BannerView(context.getActivity());
    hwBannerView.setAdId("testw6vs28auh3");
    hwBannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_320_50);
    hwBannerView.setBannerRefresh(30);

    hwBannerView.setBackgroundColor(Color.BLUE);

    textView = new TextView(context.getActivity());
    height = AdaptiveBannerHeight();
    int total = height;
    if (initialLayoutComplete == false) {
        FrameLayout.LayoutParams pa = new FrameLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, total);
        ColorValue = Color.BLACK;

        if (position.trim().equals("BOTTOM") || position.trim().equals("bottom") || position.trim().equals("Bottom")) {
            pa.gravity = Gravity.BOTTOM;
        } else {
            pa.gravity = Gravity.TOP;
        }

        frameLayout = new FrameLayout(context.getActivity());
        frameLayout.setBackgroundColor(ColorValue);
        context.getActivity().addContentView(frameLayout, pa);

        textView.setText("Banner Ad");
        textView.setTextSize(16);
        textView.setGravity(Gravity.CENTER);
        textView.setTextColor(Color.WHITE);
        frameLayout.addView(textView);

        // Create LayoutParams for the hwBannerView
        FrameLayout.LayoutParams bannerParams = new FrameLayout.LayoutParams(
                BannerAdSize.BANNER_SIZE_320_50.getWidthPx(context.getActivity()),BannerAdSize.BANNER_SIZE_320_50.getHeightPx(context.getActivity()));

        bannerParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
        frameLayout.addView(hwBannerView, bannerParams);

        frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        if (!initialLayoutComplete) {
                            initialLayoutComplete = true;
                            loadBanner(bannerKey); // Call loadBanner after the layout is complete
                        }
                    }
                });

    }
}

private void loadBanner(String bannerKey) {
    hwBannerView.setAdListener(adListener);

    AdParam adParam = new AdParam.Builder().build();
    hwBannerView.loadAd(adParam);

}
 public AdListener adListener = new AdListener() {            @Override
 public void onAdLoaded() {
     // Called when an ad is loaded successfully.
     Toast.makeText(context.getActivity(), "inside onAdLoaded", Toast.LENGTH_LONG).show();
 }

     @Override
     public void onAdFailed(int errorCode) {
         String msg = String.valueOf(errorCode);
         // Called when an ad fails to be loaded.
         Toast.makeText(context.getActivity(), msg, Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdOpened() {
         // Called when an ad is opened.
         Toast.makeText(context.getActivity(), "inside onAdOpened", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdClicked() {
         // Called when an ad is clicked.
         Toast.makeText(context.getActivity(), "inside onAdClicked", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdLeave() {
         // Called when an ad leaves an app.
         Toast.makeText(context.getActivity(), "inside onAdLeave", Toast.LENGTH_LONG).show();
     }

     @Override
     public void onAdClosed() {
         // Called when an ad is closed.
         Toast.makeText(context.getActivity(), "inside onAdClosed", Toast.LENGTH_LONG).show();
     }
 };

public int AdaptiveBannerHeight() {

    return BannerAdSize.BANNER_SIZE_320_50.getHeightPx(context.getActivity());
}

public void Hide() {
    // banner_view.removeAllViews();
}

}

note:

thank you Huawei Support

nboy1 commented 11 months ago

Edit: now i downgrade version from 13.4.66.300 to 13.4.62.302, to can add the missing ads-template jar and it resources, and now i get a close ad button but there is no ads on banner, and when i click on that close button the app crash. image