简体   繁体   中英

Admob Adview works. Why?

Yesterday I was trying to get admob advertising to work on my new app. Unfortunately, and embarassingly I had programmed myself into a corner by using minimal XML files in my programming. (its a long story why), ie I did layouts for all views programatically in Java. Anyway, when it came to adding the AdView I had a problem since the Admob Guide assumed developers would use XML extensively. So I browsed the Admob API and blundered around and I ended up with the following:

   ad = new AdView(this);
    ad.setEnabled(true);


    sublayout.addView(ad);

    SimpleAdListener sal = new SimpleAdListener();
    sal.onReceiveAd(ad);

    ad.setAdListener(sal);

    ad.getAdListener();
    ad. setKeywords("KEYWORDS RELEVANT TO MY APP");
    ad.requestFreshAd();

My question is, is this code ok? It works. I mean, I'm displaying ads on my app (still unpublished). If anyone has experience with the Admob SDK Id like some advice.

If the ads are showing and not affecting your app in an adverse way then it should be fine. I do use xml to setup my adviews, but this shoudl work also.

I can say that the admob library does a good job of using the logcat. Run your app and watch the logcat. If there are any issues or conflicts they should show up in there.

you can drop the following lines:

ad.setEnabled(true);
SimpleAdListener sal = new SimpleAdListener();
sal.onReceiveAd(ad);
ad.setAdListener(sal);
ad.getAdListener();

This is probably a little bit confusing, but those are all associated with an internal ad listener interface that admob publishes so you can listen to what happens. What you have here essentially does nothing.

reguestFreshAd will cause the ad to be refreshed once, you can use setRequestInterval to set the adview to update after a specified number of seconds. I'm assuming, since you see the ad, elsewhere you are adding it to your view. Other than that, you should be good to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM