Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Ad (adMob) on Background Thread

I want to load my add on a background thread cause it makes the SlidingMenu laggy upon opening and closing. Should I use a Thread/Handler? Or 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());
like image 666
TheLettuceMaster Avatar asked Jun 25 '13 20:06

TheLettuceMaster


People also ask

Why are my AdMob ads not showing?

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 integrate AdMob ads?

Set up your application in your AdMob account Login to AdMob and open your dashboard. Now, go to Applications> Add an Application> Fill out the information required by AdMob> Click Add, which will generate an AdMob ID for your application. To link your newly added application to Firebase.

What is background thread in Android?

For example, if your app makes a network request from the main thread, your app's UI is frozen until it receives the network response. You can create additional background threads to handle long-running operations while the main thread continues to handle UI updates.


3 Answers

This can be achieved by loading the Ad on UI Thread by runOnUiThread

Call this from onCreate()

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

loadAd() method

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);
        }
    });
}
like image 53
zackygaurav Avatar answered Nov 16 '22 02:11

zackygaurav


You should use MobileAds.initialize() method before ads loading. After that loadAd works faster

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.
like image 42
rupashka Avatar answered Nov 16 '22 01:11

rupashka


I do not believe this can be done as all UI related stuff has to be done on the main thread. The API probably already has a thread to gets the ad on the network. If it didnt android would throw a NetworkOnMainThreadException if any network related stuff is done on the main thread

like image 38
tyczj Avatar answered Nov 16 '22 01:11

tyczj