0

I like to use the sample BannerAdListener class below. I got the example working, but now I'd like to integrate this inside my app.

How can I use this class without having to copy this into every activity? Thanks in advance!

       package com.example.nieuwssuriname;

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdSize;
    import com.google.android.gms.ads.AdView;

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.LinearLayout;
    import android.widget.Toast;

    /**
     * A more advanced {@link Activity} that embeds an AdView and implements its
     * listener and sets an AdListener.
     */
    public class BannerAdListener extends Activity {
      /** Your ad unit id*/
      private static final String AD_UNIT_ID = "ca-app-pub-8383838383838/3507107948";

      /** The log tag. */
      private static final String LOG_TAG = "BannerAdListener";

      /** The view to show the ad. */
      private AdView adView;

      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        // Set the AdListener.
        adView.setAdListener(new AdListener() {
          /** Called when an ad is clicked and about to return to the application. */
          @Override
          public void onAdClosed() {
            Log.d(LOG_TAG, "onAdClosed");
            Toast.makeText(BannerAdListener.this, "onAdClosed", Toast.LENGTH_SHORT).show();
          }

          /** Called when an ad failed to load. */
          @Override
          public void onAdFailedToLoad(int error) {
            String message = "onAdFailedToLoad: " + getErrorReason(error);
            Log.d(LOG_TAG, message);
            Toast.makeText(BannerAdListener.this, message, Toast.LENGTH_SHORT).show();
          }

          /**
           * Called when an ad is clicked and going to start a new Activity that will
           * leave the application (e.g. breaking out to the Browser or Maps
           * application).
           */
          @Override
          public void onAdLeftApplication() {
            Log.d(LOG_TAG, "onAdLeftApplication");
            Toast.makeText(BannerAdListener.this, "onAdLeftApplication", Toast.LENGTH_SHORT).show();
          }

          /**
           * Called when an Activity is created in front of the app (e.g. an
           * interstitial is shown, or an ad is clicked and launches a new Activity).
           */
          @Override
          public void onAdOpened() {
            Log.d(LOG_TAG, "onAdOpened");
            Toast.makeText(BannerAdListener.this, "onAdOpened", Toast.LENGTH_SHORT).show();
          }

          /** Called when an ad is loaded. */
          @Override
          public void onAdLoaded() {
            Log.d(LOG_TAG, "onAdLoaded");
            Toast.makeText(BannerAdListener.this, "onAdLoaded", Toast.LENGTH_SHORT).show();
          }
        });    
        // Add the AdView to the view hierarchy.
        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
        layout.addView(adView);

        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("j393eji39ns3i29")

            .build();
    // Start loading the ad in the background.
    adView.loadAd(adRequest);
  }

  @Override
  public void onResume() {
    super.onResume();
    if (adView != null) {
      adView.resume();
    }
  }

  @Override
  public void onPause() {
    if (adView != null) {
      adView.pause();
    }
    super.onPause();
  }

  /** Called before the activity is destroyed. */
  @Override
  public void onDestroy() {
    if (adView != null) {
      // Destroy the AdView.
      adView.destroy();
    }
    super.onDestroy();
  }

  /** Gets a string error reason from an error code. */
  private String getErrorReason(int errorCode) {
    String errorReason = "";
    switch(errorCode) {
      case AdRequest.ERROR_CODE_INTERNAL_ERROR:
        errorReason = "Internal error";
        break;
      case AdRequest.ERROR_CODE_INVALID_REQUEST:
        errorReason = "Invalid request";
        break;
      case AdRequest.ERROR_CODE_NETWORK_ERROR:
        errorReason = "Network Error";
        break;
      case AdRequest.ERROR_CODE_NO_FILL:
        errorReason = "No fill";
        break;
    }
    return errorReason;
  }

}

1 Answer 1

0

You can have your activities to extend your BannerAdListener, like this:

public class MyOtherActivity extends BannerAdListener{
    //More code here
}

That will copy all your behaviours from the previous class, and if you want to extend them just override the method and be sure to call your parent methods in all calls.

3
  • Thanks for your reaction! I've tried to do this but I got stuck with overiding the methods. Can you give an example please?
    – Foo
    Oct 7, 2014 at 21:08
  • What methods do I have to override? The whole 'onCreate' method which contains the listener?
    – Foo
    Oct 7, 2014 at 21:17
  • you can override them all just be sure to call the parent method on everyone for example for overriding onCreate() you should put this super.onCreate(savedInstanceState); in the first line of the overriden onCreate and then your behaviour and overriden behaviour both will be executed. Good luck
    – Eefret
    Oct 7, 2014 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.