8

I want to load my add on a background thread cause it makes the SlidingMenu laggy upon opening and closing. Should I use a Thread/Handler? Or AsyncTask?

String MY_AD_UNIT_ID = "----";
AdView adView = new AdView(getActivity(), AdSize.BANNER, MY_AD_UNIT_ID);
final LinearLayout adLayout = (LinearLayout) getActivity()
            .findViewById(R.id.adLayout);
adLayout.addView(adView);
adView.loadAd(new AdRequest());
4
  • I do not believe this can be done as all UI related stuff has to be done on the main thread Commented Jun 25, 2013 at 20:55
  • but isn't there a part of it that fetches over the network? Commented Jun 25, 2013 at 20:55
  • 1
    yes there is which is done in the API probably in a seperate thread because android will throw a NetworkOnMainThreadException if any network related stuff is done on the main thread Commented Jun 25, 2013 at 20:57
  • @tyczj ok... sounds good. You can put as answer then... Commented Jun 25, 2013 at 21:02

4 Answers 4

12

This can be achieved by loading the Ad on UI Thread by runOnUiThread

Call this from onCreate()

    Thread adThread = new Thread()
    {
        @Override
        public void run()
        {
            loadAd();
        }
    };
    adThread.start();

loadAd() method

private void loadAd()
{
    // Banner Ad
    final AdView adview = (AdView) this.findViewById(R.id.adview);

    // Request for ads
    final AdRequest adRequest = new AdRequest.Builder()
            // Time for test devices
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxx")
            .addTestDevice("xxxxxxxxxxxxx")
            .build();

    // Load Ads on UI Thread
    runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            adview.loadAd(adRequest);
        }
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

This works, but I am not sure if it actually solves the problem in question. As it runs on the UI, it still causes other UI elements to lag.
Just a note for anyone googling this: I started getting errors Performing stop of activity that is not resumed: {<your.package.name>/com.google.android.gms.ads.AdActivity} ... when using this.
It will anyways run on Background Thread because the AdMob needs to update the ads from Google's server. It is just a small tweak.
7

You should use MobileAds.initialize() method before ads loading. After that loadAd works faster

Initializes the Google Mobile Ads SDK.

Call this method as early as possible after the app launches to reduce latency on the session's first ad request.

If this method is not called, the first ad request automatically initializes the Google Mobile Ads SDK.

1 Comment

It is better to put the relevant information from the link in your answer as well so that the answer is still useful if the link someday breaks.
4

I do not believe this can be done as all UI related stuff has to be done on the main thread. The API probably already has a thread to gets the ad on the network. If it didnt android would throw a NetworkOnMainThreadException if any network related stuff is done on the main thread

1 Comment

agreed , i thought somehow we could separate the network operation and UI operation , but i was wrong .They do both in single api call .
0

it's not possible. If you do

loadAd(adRequest)

in a background thread, an exception is raised:

java.lang.IllegalStateException: #008 Must be called on the main UI thread.
  at com.google.android.gms.common.internal.Preconditions.checkMainThread(com.google.android.gms:play-services-basement@@18.6.0:3)
  at com.google.android.gms.ads.BaseAdView.loadAd(com.google.android.gms:play-services-ads-lite@@23.6.0:1)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.