Code Monkey home page Code Monkey logo

hms-ads-demo-java's Introduction

Petal Ads Sample Code (Java)

English | 中文

Contents

Introduction

The Java sample code provided by Ads Kit describes how to display ads by integrating the Petal Ads SDK into your Android app.

Installation

Install the sample code on a Huawei phone.
You can use HMS Toolkit to quickly run the sample code. HMS Toolkit supports one-stop kit integration, and provides functions such as free app debugging on remote real devices. To learn more about HMS Toolkit, please refer to the HMS Toolkit documentation.

Configuration Management

N/A

Environment Requirements

Your Huawei phone has HMS Core (APK) 4.0.0 or later installed.

Sample Code

The Java sample code provided by Ads Kit integrates the Petal Ads SDK, which offers a range of ad formats including banner, native, rewarded, interstitial, splash, and roll ads. The sample code contains the following files for you to load and display ads and obtain user consent:

(1) BannerActivity.java Used to load and display banner ads.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\BannerActivity.java

(2) NativeActivity.java Used to load and display native ads.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\NativeActivity.java

(3) RewardActivity.java Used to load and display rewarded ads.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\RewardActivity.java

(4) InterstitialActivity.java Used to load and display interstitial ads.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\InterstitialActivity.java

(5) SplashActivity.java Used to load and display splash ads.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\SplashActivity.java

(6) InstreamActivity.java Used to load and display roll ads.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\InstreamActivity.java

(7) ConsentActivity.java Used to obtain user consent.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\ConsentActivity.java

(8) ProtocolActivity.java Used to obtain users' consent on the privacy statement.
Storage path: app\src\main\java\com\huawei\hms\ads\sdk\ProtocolActivity.java

Running Result

Banner ads         Interstitial ads        Native ads

Reward ads         Roll ads        Splash ads

Technical Support

You can visit the Reddit community to obtain the latest information about HMS Core and communicate with other developers.

If you have any questions about the sample code, try the following:

  • Visit Stack Overflow, submit your questions, and tag them with huawei-mobile-services. Huawei experts will answer your questions.
  • Visit the HMS Core section in the HUAWEI Developer Forum and communicate with other developers.

If you encounter any issues when using the sample code, submit your issues or submit a pull request.

License

The sample code is licensed under Apache License 2.0.

hms-ads-demo-java's People

Contributors

chenqiuwei avatar ferprieto avatar liuxiaohong414 avatar mike-mei avatar mwshubham avatar songshang110 avatar tiantian0303 avatar wwx5325878 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hms-ads-demo-java's Issues

from where i get the id ads?

hi where i can find id ads like admob id adverist ca-app/xxxxxxxxxxx-xxxxxxxx
because i need change them with real ads not test ads ?? give me the url please and thank you

Banner ad doesn't work in android library

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:

  • i've tried using XML Source file too but nothing same result, i give it height and background color just to debug if bannerview show up in my activity. i've tried build banner before initial the layout too but same result too no banner ad, sometime it's gives me code error 4 and sometimes 3, i think it's just when time to refreshing the ad it calls the listeners.
  • interstitial work fine,
  • i've added necessary declarations needed for huawei ads jar's and resources in xml file

thank you Huawei Support

Error code 2

I recent download this example code, and everytime return error code 2

Violation about Preventing invalid impressions

I used the same code demo from Native Ads in my app and the banner displays correctly but when I published it on AppGallery I got the following message:

An ad cannot be displayed repeatedly for one request. After being displayed, the ad needs to be loaded again in real time for the next request.
[Recommended] The ad object on each page is independent. You need to use the createXX method to create ads and register a callback.

Please, has anyone had the same problem with this code being used?

Ad load failed with error code: 3 for Splash Ad

Hello there.
I am trying to run Splash Ad with test Ids but I am getting error code 3.
I've deleted HMS Core cache, did not work.
I've completely factory reset my phone, still the same issue.
I can confirm I have an active network because I open other ads and they are working.

I am using Huawei P40 Lite with Android 10 Installed. HMS Core version is 6.1.0.313

Here is my logcat.txt
Here is the live video from the device.

Thanks

Ad load failed with error code: 3

Hi,

I have problems with displaying of Native, Reward and Interstitial Ads - all of them return "Ad load failed with error code: 3", meaning "No ad found".

Logcat states:
"HiAdSDK.InterstitialAdManager: onAdFailed, errorCode:204 "

I understand, that there may be no regular ads available, however those are test ads that I would expect to be always ready, for testing purposes.
Is there any way to have a reliable source of test ads for, well, testing?

APK link for the Ads Demo

Hi team,

Can we have support to automatically generate APK file for each release of this demo project via Github Actions.

This will help the developers to review the APK directly without going into building the entire project.

Regards,
Shubham

InstreamAdsLoader compatible with ExoPlayer?

Is the InstreamAdsLoader compatible with ExoPlayer for instream pre roll ads? I would like to integrate it with Exoplayer to monetize my app on all android devices.

thank you.

Ad failed with error code 0 - onAdFailed error code 499

Hello.

I am testing your apk on EMUI 8 and Android version 8 tablet that has HMS Core 6.0.0.304.
I am getting Ad failed with error code 0 - onAdFailed error code 499 for all ad types.
It works for EMUI & Android 10 Version but doesn't work below that.
When I check the official page it says EMUI 3.0 or later is enough.
image

Here is the log:
com.huawei.hms.ads.sdk-logcat.txt

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.