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), i.e. 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.
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.
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.
精彩评论