12

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

Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32
Addev
  • 31,819
  • 51
  • 183
  • 302

5 Answers5

4

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);
d.moncada
  • 16,900
  • 5
  • 53
  • 82
  • But in the documentation, they said "Note: Make all calls to the Mobile Ads SDK on the main thread." https://developers.google.com/ad-manager/mobile-ads-sdk/android/native/start#build_an_adloader – baderkhane Feb 29 '20 at 22:04
  • @baderkhane UI thread is main thread of Applications... I have another solution look my answer – Ucdemir Feb 08 '21 at 08:12
1

Here is my solution:

  public class YHYBackgroundThread extends AsyncTask<Object, Object, Object> {
        private YHYBackgroundListener mListener;
        private Context context;
    
        public YHYBackgroundThread(Context context) {
    
            this.context= context;
    
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
    
    
    
        }
    
        @Override
        protected Object doInBackground(Object... params) {
    
            if(mListener != null){
                return  mListener.onBackground(params);
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(Object t) {
            if (mListener != null) {
    
    
                mListener.onPostExecute(t);
                
            }
        }
    
        public YHYBackgroundThread setListener(YHYBackgroundListener mListener){
    
            this.mListener = mListener;
    
            return this;
        }
    
    }

YHYBackgroundListener

public interface YHYBackgroundListener {

    Object onBackground(Object... params);
    void onPostExecute(Object list);
}

call this, when you need show ads

   new YHYBackgroundThread(context).setListener(


                    new YHYBackgroundListener() {
                        @Override
                        public Object onBackground(Object... params) {

                            AdRequest request = new AdRequest.Builder().build();


                            return request;
                        }

                        @Override
                        public void onPostExecute(Object list) {

                            AdRequest request = (AdRequest) list;
                            adView.loadAd(request);
                        }
                    }
            ).execute();
Ucdemir
  • 2,852
  • 2
  • 26
  • 44
1

Use threads:

public class LayoutTestActivity extends Activity {

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

    long now = System.currentTimeMillis();

    new Thread(new Runnable() {

        public void run() {
            new AdView(this, AdSize.BANNER, "MY_ID");               
        }
    }).start();

    Log.e("Admob Test","The UI was blocked "+(System.currentTimeMillis()-now)+"ms");
}
dirong
  • 156
  • 1
  • 6
  • Your code need a Looper preparation but yes is the easier sollution. Anyway is curious the google product blocks the UI so much time – Addev Feb 27 '12 at 21:49
0

In my case I requested too much advertisement items in one request. The heap is overloaded and GC started action and it blocked my Main thread. The piece of advice try to avoid requesting too much Ad in one request.

adLoader.loadAds(AdRequest.Builder().build(), 1)
Kirk_hehe
  • 449
  • 7
  • 17
  • Adloader seems only for Native ads https://developers.google.com/android/reference/com/google/android/gms/ads/AdLoader – Ucdemir Feb 08 '21 at 07:32
0

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

chimitos
  • 79
  • 9
Serdar Dogruyol
  • 5,147
  • 3
  • 24
  • 32