Android 在每个活动中添加横幅,而不复制代码

Android 在每个活动中添加横幅,而不复制代码,android,adsense,Android,Adsense,我喜欢使用下面的示例BannerAdListener类。我让这个例子起作用了,但现在我想把它集成到我的应用程序中 如何使用该类而不必将其复制到每个活动中?提前谢谢 package com.example.nieuwssuriname; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.and

我喜欢使用下面的示例BannerAdListener类。我让这个例子起作用了,但现在我想把它集成到我的应用程序中

如何使用该类而不必将其复制到每个活动中?提前谢谢

       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;
  }

}

您可以通过活动来扩展您的听众,如下所示:

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

这将复制上一个类中的所有行为,如果要扩展它们,只需重写该方法,并确保在所有调用中调用父方法

谢谢你的反应!我曾尝试过这样做,但我被过度使用这些方法所困扰。你能举个例子吗?我必须重写哪些方法?包含侦听器的整个“onCreate”方法?您可以全部重写它们,只需确保对每个人调用父方法即可。例如,要重写onCreate,您应该将super.onCreatesavedInstanceState;在重写onCreate的第一行中,然后将执行您的行为和重写的行为。祝你好运