Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
244 views
Welcome To Ask or Share your Answers For Others

1 Answer

I had a similar issue. Resolved it by delaying the ad-request for 1 second (which gives time for the AdView to load and not block the UI.

        new Timer().schedule(new TimerTask()
        {
            @Override
            public void run()
            {
                MainActivity.runOnUiThread(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        AdRequest adRequest = new AdRequest.Builder()
                                .addTestDevice(AD_TEST_DEVICE)
                                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                                .build();

                        adView.loadAd(adRequest);
                    }
                });
            }
        }, 1000);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...