/snakat-ads-android

Ads helper.

Primary LanguageJavaMIT LicenseMIT

snakat-ads-android

Installation

  1. Add this to your project as a git submodule
cd ~/sample_app/
git submodule add https://github.com/khanhbui/snakat-ads-android.git snakat-ads
  1. Create a file, named config.gradle, which defines sdk versions, target versions and dependencies.
ext {
    plugins = [
            library: 'com.android.library'
    ]

    android = [
            compileSdkVersion: 31,
            buildToolsVersion: "31.0.0",
            minSdkVersion    : 14,
            targetSdkVersion : 31
    ]

    dependencies = [
            appcompat: 'androidx.appcompat:appcompat:1.4.1',
            ads: 'com.android.billingclient:billing:4.1.0'
    ]
}
  1. Add this line on top of build.gradle
apply from: "config.gradle"
  1. Add this line to settings.gradle
include ':snakat-ads'
  1. Add this line to dependencies section of app/build.gradle
implementation project(path: ':snakat-ads')
  1. Add your AdMob app ID (identified in the AdMob UI) to your app's AndroidManifest.xml file
<manifest>
    <application>
      <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy" />
    </application>
</manifest>

Usage

Initialization

public class App extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        Context context = getApplicationContext();
        AdsManager.createInstance(context);
    }

    @Override
    public void onTerminate() {
        AdsManager.destroyInstance();

        super.onTerminate();
    }
}

If you want to see logs while developing your app. Please use this.

AdsManager.createInstance(context, true);

Show a banner

ViewGroup container = ...;
String adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/zzzzzzzzzz";

AdsManager.getInstance()
  .showBanner(container, adUnitId)
  .subscribe(new Consumer<AdsEvent>() {
    @Override
    public void accept(AdsEvent adsEvent) throws Exception {
      switch (adsEvent.getType()) {
        case FAILED_TO_LOAD:
          // Ad failed to load.
          break;
        case LOADED:
          // Ad finishes loading.
          break;
        case CLICKED:
          // The user clicks on an ad.
          break;
        case DISMISSED:
          // The user is about to return to the app after tapping on an ad.
          break;
      }
    }
  })

Show an interstitial

Activity activity = ...;
String adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxx/zzzzzzzzzz";

AdsManager.getInstance()
  .showInterstitial(activity, adUnitId)
  .subscribe(new Consumer<AdsEvent>() {
    @Override
    public void accept(AdsEvent adsEvent) throws Exception {
      switch (adsEvent.getType()) {
        case LOADED:
          // Ad finishes loading done.
          break;
        case CLICKED:
          // The user clicks on an ad.
          break;
        case DISMISSED:
          // Fullscreen content is dismissed.
          break;
      }
    }
  }, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
      // Handle the error occurs when loading or showing ad.
    }
  });

License

MIT License

Copyright (c) 2022 Khanh Bui

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.