Android Open Source - Android-Lib-Advertising Main Activity






From Project

Back to project page Android-Lib-Advertising.

License

The source code is released under:

Apache License

If you think the Android project Android-Lib-Advertising listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package android.lib.ad.sample;
//from   w ww  .  j a  va  2  s.c  o m
import android.app.Activity;
import android.lib.ad.AdService;
import android.os.Bundle;
import android.view.View;

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

import com.sec.android.ad.AdHubView;

public final class MainActivity extends Activity {
    // Keeps an instance of AdService for housekeeping (onResume, onPause and onDestroy).
    private AdService adService;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.setContentView(R.layout.activity_main);

        // Calls this method to enable advertising.
        this.enableAdvertising();
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (this.adService != null) {
            // If you want to display Google AdMob ads, resume it here.
            // This is necessary for Google AdMob ads. You can call this method even if you are not displaying ads from Google.
            ((AdView)this.findViewById(R.id.adview_google)).resume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (this.adService != null) {
            // If you are displaying Google AdMob ads, pause it here to reduce network usage.
            // This is necessary for Google AdMob ads. You can call this method even if you are not displaying ads from Google.
            ((AdView)this.findViewById(R.id.adview_google)).pause();
        }
    }

    @Override
    protected void onDestroy() {
        if (this.adService != null) {
            // If you are displaying Google AdMob ads, destroy it here to clean up any resources used by it.
            // This is necessary for Google AdMob ads. You can call this method even if you are not displaying ads from Google.
            ((AdView)this.findViewById(R.id.adview_google)).destroy();
        }

        super.onDestroy();
    }

    // This example shows an attempt to display an ad from Samsung first. If it fails, we make a second attempt to display an ad from Google.
    private void enableAdvertising() {
        this.adService = new AdService(this);

        // Sets this to your Google AdMob AD_UNIT_ID or Samsung AdHub App/Site ID.
        // Here we set it to Samsung in our first attempt. If it fails, we will switch to Google later.
        this.adService.setAdId("YOUR_SAMSUNG_AD_ID");

        this.adService.setAdListener(new AdService.AdListener() {
            @Override
            public void onAdLoaded(final AdView adView) {
                // Displays the ad view after an ad is loaded successfully.
                MainActivity.this.findViewById(R.id.adview_google).setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdLoaded(final AdHubView adView) {
                // Displays the ad view after an ad is loaded successfully.
                MainActivity.this.findViewById(R.id.adview_samsung).setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(final AdView adView, final int errorCode) {
                // Hides the ad view if it has failed to load an ad.
                MainActivity.this.findViewById(R.id.adview_google).setVisibility(View.GONE);
            }

            @Override
            public void onAdFailedToLoad(final AdHubView adView, final Exception exception) {
                // Hides the ad view if it has failed to load an ad.
                MainActivity.this.findViewById(R.id.adview_samsung).setVisibility(View.GONE);

                // Switches to Google AdMob if Samsung AdHub failed to provide us an ad.
                MainActivity.this.adService.setAdId("YOUR_GOOGLE_AD_ID");

                // Tries to display an ad from Google in our second attempt.
                MainActivity.this.adService.displayAd((AdView)MainActivity.this.findViewById(R.id.adview_google));
            }
        });

        // Calls either one method below to display an ad from Google AdMob or Samsung AdHub.

        // Displays an ad from Samsung AdHub.
        this.adService.displayAd((AdHubView)this.findViewById(R.id.adview_samsung), AdService.getBestFitAdSize(this));

        // Displays an ad from Google AdMob.
        //this.adService.displayAd((AdView)this.findViewById(R.id.adview_google));
    }
}




Java Source Code List

android.lib.ad.AdService.java
android.lib.ad.sample.MainActivity.java