private InterstitialAd admobAd2;
public void loadAds2(final Context context) {
new Thread() {
public void run() {
AdRequest.Builder builder = new AdRequest.Builder();
AdRequest adRequest = builder.build();
admobAd2 = new InterstitialAd(context);
admobAd2.setAdUnitId(context.getResources().getString(R.string.admobUnitId));
admobAd2.loadAd(adRequest);
}
}.start();
}
private class InterstitialTask extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(Void... params) {
try {
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(getApplicationContext());
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));
Thread.sleep(50);
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
}
@Override
public void onAdLoaded() {
publishProgress(100); //Random value here
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
return "All Done!";
}
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
if (values[0] == 100) {
mInterstitialAd.show();
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
Log.d("onPostExecute"," onPostExecute...");
// Request a new ad if one isn't already loaded, hide the button, and kick off the timer.
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
}
}