0

My problem that my live wallpaper displays some ad in the settings screen in a linearlayout (it works perfectly), but when I send it to background (e.g.: press home button), it seems that the adview is not destroyed and that causes higher cpu usage (25-50%). If I turn off my internet connection or just remove the ad displaying code it works without spinning. After I investigated the question, I found (https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals) that I had to destroy the adview at onDestroy, but my problem is that this should happen in the activity where I can't reach the adview. And I don't know how I could solve it so if you have any idea please help me. My code: AdPreference.java:

public class AdPreference extends Preference {
public AdPreference(Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle);}
public AdPreference(Context context, AttributeSet attrs) {super(context, attrs);}
public AdPreference(Context context) {super(context);}
public AdView adView;
@Override  
protected View onCreateView(ViewGroup parent) {
  // this will create the linear layout defined in ads_layout.xml
  View view = super.onCreateView(parent);

  // the context is a PreferenceActivity
  Activity activity = (Activity)getContext();


  // Create the adView
  adView = new AdView(activity, AdSize.BANNER, "mybanner");
  ((LinearLayout)view).addView(adView);

  // Initiate a generic request to load it with an ad
  AdRequest request = new AdRequest();
  adView.loadAd(request);                 
  return view;    
  }

and my activity: public class Prefs extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

 @Override
 protected void onCreate(Bundle icicle) {
 super.onCreate(icicle);
 addPreferencesFromResource(R.xml.wallpaper_settings);   

  }
 @Override
 protected void onDestroy()
{               
 getPreferenceManager().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
 super.onDestroy();
}

public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key)
{
} }

1 Answers1

0

I could be wrong because I am not the best at this, but it looks like you might be missing a

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

Or something like that. I mean, I don't see really anywhere that you tell the live wallpaper to stop the ad requests.

Like I said, I could be wrong, but those are my beliefs on the matter at hand.

vamp6x6x6x
  • 65
  • 1
  • 2
  • 8