JMazApps
JMazApps

Reputation: 41

AdMob Android requesting ads while program is not in view

Hello I am having a problem trying to stop adMob from requesting new ads. If a person hits the back key or home the application goes to sleep, but admob continues to request new ads. The only way it stops is if the user selects quit from the menu.

Does anyone know of a way that in my onPause method I could do something such as ad.pauseUpdates(); I am not seeing anything like this in the documentation.

Any ideas would be helpful.

Upvotes: 4

Views: 2319

Answers (1)

mostafa_zakaria
mostafa_zakaria

Reputation: 116

You can do that:

public class BannerSample extends Activity {
/** The view to show the ad. */
  private AdView adView;    

  /* Your ad unit id. Replace with your actual ad unit id. */
  private static final String AD_UNIT_ID = "INSERT_YOUR_AD_UNIT_ID_HERE";

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

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

// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
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("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
    .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() {
// Destroy the AdView.
 if (adView != null) {
  adView.destroy();
  }
 super.onDestroy();
}
  }

Upvotes: 1

Related Questions