Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 905
Addev Avatar asked Feb 27 '12 21:02

Addev


People also ask

Why did my ads stop showing AdMob?

Ads won't show if you haven't integrated the Google Mobile Ads SDK correctly. Is your ad implementation code working properly? Test your implementation code to check that ads can show. You can also use ad inspector to test your app's ad serving.

How do I control AdMob ads?

Click Blocking controls under your app's name in the sidebar. Note: All blocking controls at the app level, except for the Ad review center, apply only for the app that the settings were saved for. All creatives that are blocked in the Ad review center are blocked for the entire AdMob account.


2 Answers

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();
like image 142
Ucdemir Avatar answered Sep 29 '22 00:09

Ucdemir


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);
like image 43
d.moncada Avatar answered Sep 28 '22 23:09

d.moncada