This question already has an answer here:
- Android, AdMob: AdMob ad refresh destroys frame rate 2 answers
I'm using Google Admob SDK v6.1.0 (https://developers.google.com/mobile-ads-sdk/download), and I instantiate the com.google.ads.AdView programmatically (not in XML), and add it into a LinearLayout, dynamically in my Activity.
One of my users reported that when they click the Home button while in my Activity (in order to background it), they start seeing high CPU usage sourced to my app. I was able to reproduce this on a Jellybean platform, and noticed that the source for high CPU usage was a WebViewCoreThread.
My Activity doesn't use any WebViews at all, but I was able to step through my Activity's initialization, and noticed that this WebViewCoreThread starts when I instantiate the AdMob AdView object. As state in the AdMob's references, I call destroy() on this AdView in my Activity's onDestroy() method. And I aso changed my code to call AdView.onDestroy() in my onPause() method. But nothing seems to be causing the WebViewCoreThread to stop. I guess, I'm okay if that thread sticks around. But if I start my Activity several times over and over again, this thread starts using anywhere between 8 to 25% of my CPU, even my activity is not in the foreground.
I noticed a few other users saying that you must call WebView.onPause() as a corrective action. (http://stackoverflow.com/questions/2040963/webview-threads-never-stop-webviewcorethread-cookiesyncmanager-http0-3) But this isn't directly possible for me, since my web view is created by AdMob's AdView. I also changed my code to call .removeAllViews() for mt Admob AdView's container LinearLayout object, and then call System.gc() to force garbage collection, but nothing seems to kill my WebViewCoreThread and eventually it starts eating up the CPU until I force-kill my app's process.
Any clues why AdMob is doing this, and how I can force this thread to be killed?
I'm attaching a class that I created to encapsulate AdView creation and destruction. I call this class's getNewAd() method in my activity's initialization. And I call this class's removeAd() in my Activity's onPause() and onDestroy() methods:
package com.shiprack.client;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.mobclix.android.sdk.Mobclix;
import com.mobclix.android.sdk.MobclixMMABannerXLAdView;
import android.app.Activity;
import android.view.Gravity;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
public class AdManager {
public AdManager(EventLog logger, LinearLayout container, Activity activity) {
_container = container;
_activity = activity;
_eventLogger = logger;
}
public void setNetwork(int network) {
_network = network;
}
public void getNewAd() {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
params.gravity = Gravity.CENTER;
switch (_network) {
case TrackDatabase.AD_NETWORK_ADMOB: {
_admobBanner = new AdView(_activity, AdSize.BANNER, "a14dc419375634c");
_container.addView(_admobBanner, params);
_admobBanner.loadAd(new AdRequest());
break;
}
case TrackDatabase.AD_NETWORK_MOBCLIX: {
Mobclix.onCreate(_activity);
_mobclixBanner = new MobclixMMABannerXLAdView(_activity);
_container.addView(_mobclixBanner, params);
_mobclixBanner.getAd();
break;
}
}
}
public void removeAd() {
switch (_network) {
case TrackDatabase.AD_NETWORK_ADMOB: {
_admobBanner.destroy();
break;
}
case TrackDatabase.AD_NETWORK_MOBCLIX: {
_mobclixBanner.cancelAd();
break;
}
}
_container.removeAllViews();
}
private EventLog _eventLogger;
private LinearLayout _container;
private Activity _activity;
private AdView _admobBanner;
private MobclixMMABannerXLAdView _mobclixBanner;
private int _network;
}