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.
Is there some way of avoiding that?
Thanks
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.
Click Blocking controls under your app's name in the sidebar. Note: All blocking controls at the app level, except for the Ad review center, apply only for the app that the settings were saved for. All creatives that are blocked in the Ad review center are blocked for the entire AdMob account.
Here is my solution:
public class YHYBackgroundThread extends AsyncTask<Object, Object, Object> {
private YHYBackgroundListener mListener;
private Context context;
public YHYBackgroundThread(Context context) {
this.context= context;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected Object doInBackground(Object... params) {
if(mListener != null){
return mListener.onBackground(params);
}
return null;
}
@Override
protected void onPostExecute(Object t) {
if (mListener != null) {
mListener.onPostExecute(t);
}
}
public YHYBackgroundThread setListener(YHYBackgroundListener mListener){
this.mListener = mListener;
return this;
}
}
YHYBackgroundListener
public interface YHYBackgroundListener {
Object onBackground(Object... params);
void onPostExecute(Object list);
}
call this, when you need show ads
new YHYBackgroundThread(context).setListener(
new YHYBackgroundListener() {
@Override
public Object onBackground(Object... params) {
AdRequest request = new AdRequest.Builder().build();
return request;
}
@Override
public void onPostExecute(Object list) {
AdRequest request = (AdRequest) list;
adView.loadAd(request);
}
}
).execute();
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With