开发者

admob: How to load AdRequest outside of UI Thread?

开发者 https://www.devze.com 2023-03-07 03:36 出处:网络
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.

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 a开发者_开发问答n 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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号