Skip to content
Advertisement

How to avoid Admob blocking the UI thread

I have detected some of my activities are blocked at the launch. So I wrote that code in a new project:

public class LayoutTestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        long now = System.currentTimeMillis();

        new AdView(this, AdSize.BANNER, "MY_ID");

        Log.e("Admob Test","The UI was blocked "+(System.currentTimeMillis()-now)+"ms");
    }
}

And the result is that the first creation of an AdView object blocks the UI thread for between 1 and 2 seconds.

Is there some way of avoiding that?

Thanks

Advertisement

Answer

You are creating your AdView in your UI thread which is the reason for getting blocked. While the AdView initilization takes place the thread wont do anything else.

You can try loading your AdView in another thread or can use a AsyncTask to load it in a UI safe way.

Check this for more info about AsyncTask and Threading in Android.

http://developer.android.com/reference/android/os/AsyncTask.html

Advertisement