如何避免Admob阻塞UI线程

时间:2022-04-07 00:17:10

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.

结果是,AdView对象的第一次创建会阻塞UI线程1到2秒。

Is there some way of avoiding that?

有什么办法可以避免这种情况吗?

Thanks

谢谢

3 个解决方案

#1


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.

您正在UI线程中创建AdView,这是阻塞的原因。当AdView初始化发生时,线程不会执行任何其他操作。

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

您可以尝试在另一个线程中加载AdView,或者可以使用AsyncTask将其加载到UI安全的方式中。

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

有关Android中的AsyncTask和线程的更多信息,请查看这里。

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

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

#2


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.

我也有类似的问题。通过将ad请求延迟1秒来解决这个问题(这给了AdView加载而不是阻塞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);

#3


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

#1


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.

您正在UI线程中创建AdView,这是阻塞的原因。当AdView初始化发生时,线程不会执行任何其他操作。

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

您可以尝试在另一个线程中加载AdView,或者可以使用AsyncTask将其加载到UI安全的方式中。

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

有关Android中的AsyncTask和线程的更多信息,请查看这里。

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

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

#2


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.

我也有类似的问题。通过将ad请求延迟1秒来解决这个问题(这给了AdView加载而不是阻塞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);

#3


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