1

Can I load admob ad using AsyncTask ?

I have tried it but I get lots of warning at runtime and the ad is not loaded

one of the warning when I create new AdView instance is

07-06 09:57:31.170: W/webview(1113): java.lang.Throwable: Warning: A WebView method was called on thread 'AsyncTask #1'. All WebView methods must be called on the UI thread. Future versions of WebView may not support use on other threads.

my method looks like this

 @Override
protected AdView doInBackground(Activity... activityParam) 
{   
         Looper.prepare();              
         activity = activityParam[0];   
         // Create the adView
         AdView adView = new AdView(activity, AdSize.BANNER, "xxxxxxxxxxxxx");                  
         return adView;         
}

Please advise if this should at all be called on non UI thread, the only reason I am doing it to prevent UI blocking..

Edit.

May be I did not express my problem properly,

I am trying to load the ad in background because it seems to block for some seconds, I do not want to block my activity's interface.

read this post How to avoid Admob blocking the UI thread where it is recommended that to avoid UI blocking one should use AsyncTask

So what I want to know if is alternate to load an ad in the background.. ?

Community
  • 1
  • 1
Ahmed
  • 14,503
  • 22
  • 92
  • 150
  • I would just follow the sample and add the adview in the UI thread and not put it in an AsyncTask. – Morrison Chang Jul 06 '12 at 05:08
  • @MorrisonChang I did that initially but then I saw a bit of lag then the post I mentioned in my question showed up which recommended it be done on AsyncTask – Ahmed Jul 06 '12 at 05:22

3 Answers3

3

The problem is that you use the view in the doInBackground method that is not in the UI thread. Just hanlde your view in onPostExecute (called after doInBackground) or in onPreExecute (called before doInBackground) methods of the AsyncTask which are called in the UI thread.

AggelosK
  • 4,313
  • 2
  • 32
  • 37
1

Separate out the time required to create the AdView which needs to be done on the UI thread and the time it takes to communicate to the ad server and deliver an ad. The 'block' or 'lag' is probably the Ad SDK's thread trying to get an ad to show to you. As it has to do this over the internet it takes time for an ad to show up. I really don't think AdMob would block the UI thread while trying to get an ad, however there will be a visible delay between when you see all of your UI widget items (buttons, textfields, images, etc.) shown on the screen of your activity and when the ad shows up.

Morrison Chang
  • 11,691
  • 3
  • 41
  • 77
0

Admob banners and interstitials will be only shown if the code is handled within the UI thread.

Depending on your performance needs you might want to check out the following post. It includes a very good summary of why using AsyncTask is less efficient than creating your own thread directly: Android basics: running code in the UI thread

In the following link you can find a tutorial of how to use interstitials within a 30 seconds recurrent job using threads: http://www.6020peaks.com/2015/01/how-to-use-admob-interstitials-from-a-non-ui-thread-in-android/

Community
  • 1
  • 1
narko
  • 3,645
  • 1
  • 28
  • 33