8

アドオンをバックグラウンド スレッドにロードしたいのでSlidingMenu、開閉時に遅延が発生します。Thread/を使用する必要がありHandlerますか? それともAsyncTask

String MY_AD_UNIT_ID = "----";
AdView adView = new AdView(getActivity(), AdSize.BANNER, MY_AD_UNIT_ID);
final LinearLayout adLayout = (LinearLayout) getActivity()
            .findViewById(R.id.adLayout);
adLayout.addView(adView);
adView.loadAd(new AdRequest());
4

3 に答える 3

12

これは、次の方法で UI スレッドに広告をロードすることで実現できます。runOnUiThread

からこれを呼び出しますonCreate()

    Thread adThread = new Thread()
    {
        @Override
        public void run()
        {
            loadAd();
        }
    };
    adThread.start();

loadAd()方法

private void loadAd()
{
    // Banner Ad
    final AdView adview = (AdView) this.findViewById(R.id.adview);

    // Request for ads
    final AdRequest adRequest = new AdRequest.Builder()
            // Time for test devices
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("xxxxxxxxxxxxxxxxxxxxxxx")
            .addTestDevice("xxxxxxxxxxxxx")
            .build();

    // Load Ads on UI Thread
    runOnUiThread(new Runnable()
    {
        @Override
        public void run()
        {
            adview.loadAd(adRequest);
        }
    });
}
于 2015-08-10T21:34:06.043 に答える
6

広告を読み込む前にMobileAds.initialize()メソッドを使用する必要があります。その後、loadAd はより高速に動作します

Initializes the Google Mobile Ads SDK.

Call this method as early as possible after the app launches to reduce latency on the session's first ad request.

If this method is not called, the first ad request automatically initializes the Google Mobile Ads SDK.
于 2017-05-26T17:43:09.373 に答える
3

すべての UI 関連のものはメイン スレッドで実行する必要があるため、これが実行できるとは思いません。API には、ネットワーク上で広告を取得するためのスレッドが既にある可能性があります。NetworkOnMainThreadExceptionメインスレッドでネットワーク関連の処理が行われていなければ、Android は をスローします。

于 2013-06-25T21:03:59.167 に答える