Loading Ad (adMob) on Background Thread
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);
}
});
}
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.
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