5

I have a game which has ads in it.I eventually found the source of lag is basically because of the adRequest process which takes a long time.

super.onCreate(savedInstanceState){

    mainLayout = new LinearLayout(this);
    mainLayout.setOrientation(LinearLayout.VERTICAL);
    adView = new AdView(this, AdSize.BANNER, "MY_ID");
    adView.setVisibility(AdView.VISIBLE);
    mainLayout.addView(adView); 
    adView.loadAd(new AdRequest());
     //more codes below 
    }

I tried to do something like creating a thread which would do some loadAd when there's an adRequest. but that resulting in ads won't show up. So I think the loadAd request must be done in UI Thread. Is there any workaround about this? I still don't understand about how UI Thread works anyway

Fugogugo
  • 4,460
  • 10
  • 36
  • 50
  • Exactly same thing. I use `play-services-ads:8.4.0` and first time when I call `adView.loadAd(...)` my app freeze for 100-500ms. And I can't just move it to another thread due to `Can't create handler inside thread that has not called Looper.prepare()` limitation – IliaEremin Mar 16 '16 at 09:45

1 Answers1

-2

Try using an AsyncTask(). You can launch it when you need an add and it will update in the background. When it's done you can present the result back to the UI thread.

While not specifically for ads, Java Code Geeks has a great representation of this http://www.javacodegeeks.com/2011/05/android-json-gson-revisited.html

Bill Mote
  • 12,644
  • 7
  • 58
  • 82
  • well there.. I thought that would be the same as I create a thread to handle the request. I've done it before . Trying to adView.loadAd(new AdRequest()) in that thread, and there it is, no ads shown . – Fugogugo May 21 '11 at 01:55
  • 1
    Runnable runnable = new Runnable(); might be good Java, but it's bad mobile. – Bill Mote May 07 '13 at 15:27