How to avoid Admob blocking the UI thread

后端 未结 4 1572
忘了有多久
忘了有多久 2020-12-16 18:04

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 {

          


        
4条回答
  •  伪装坚强ぢ
    2020-12-16 18:43

    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);
    

提交回复
热议问题