问题
I am trying to setup adMob ads. I have two questions:
1) Am I using adListener interface correctly? 2) How come I am unable to see ad Interstitials?
Lets start with the adListener. As far as what I understood from the instructions, adListener is an interface. So I created an interface.
import com.google.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public interface AdListener {
public void onReceiveAd(AdView ad);
public void onFailedToReceiveAd(AdView ad, AdRequest.ErrorCode error);
public void onPresentScreen(AdView ad);
public void onDismissScreen(AdView ad);
public void onLeaveApplication(AdView ad);
}
Then in my main class I implemented AdListener, and once I did I was prompted to add unimplemented methods (all the methods from the interface).
public class MainActivity extends Activity implements AdListener {
// other code for the body (here). Just showing the methods and implement
@Override
public void onReceiveAd(AdView ad) {
Log.d(TAG, "onReceiveAd");
Toast.makeText(this, "onReceiveAd", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailedToReceiveAd(AdView ad, ErrorCode error) {
Log.d(TAG, "onFailedToReceiveAd");
Toast.makeText(this, "onFailedToReceiveAd", Toast.LENGTH_SHORT).show();
}
@Override
public void onPresentScreen(AdView ad) {
Log.d(TAG, "onPresentScreen");
}
@Override
public void onDismissScreen(AdView ad) {
Log.d(TAG, "onDismissScreen");
}
@Override
public void onLeaveApplication(AdView ad) {
Log.d(TAG, "onLeaveApplication");
}
}
My reason for thinking this does nothing is that I am not receiving my logs. I added toasts to double confirm I was not missing something. I think there is another step I am missing for this to work.
Now for the ads part. I thought I followed the instructions well, and my banners work in fact. But my ad interstitials do not work. This is what I have for the banners and the interstitials.
Starting with code snippet from my xml for the banner
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="I_ADDED_CORRECT_ADUNIT_ID"
ads:adSize="BANNER" />
In my main Activity, I have the following code. Again, I want to see test ads:
// setup ad banner and interstitial
final TelephonyManager tm = (TelephonyManager)getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String testDeviceId = tm.getDeviceId();
Log.d(TAG, "testDeviceId retrieved(" + testDeviceId + ")");
// create interstitial
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("I_ADDED_CORRECT_ADUNIT_ID");
// ad banner
adView = (AdView)this.findViewById(R.id.adView);
// request test interstitial ads
AdRequest adRequestInterstitial = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(testDeviceId)
.build();
// request test banner ads
AdRequest adRequestBanner = new AdRequest.Builder()
.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
.addTestDevice(testDeviceId)
.build();
//load interstitial ads
interstitial.loadAd(adRequestInterstitial);
displayInterstitial();
// load banner ads
adView.loadAd(adRequestBanner);
//NOTE: the above code is all in onCreate().
//diaplayInterstitial is outside of onCreate()
public void displayInterstitial() {
if (interstitial.isLoaded()) {
Log.d(TAG, "displayInterstitial()");
interstitial.show();
}
}
Lets see what else to tell you? Awww, yes! I also have the permissions already set in the manifest. Permissions = INTERNET, ACCESS_NETWORK_STATE, ACCESS_WIFI_STATE, READ_PHONE_STATE.
I went to project properties and added the google-play-services_lib
This is my whole setup! I have tried a number of things, because I had some 'feelings' about what may be the issue. First, I thought that maybe test ad banners and test ad interstitials cannot be requested at the same time. So I moved interstitial code to onStart(). But then, I was not sure if this was necessary, beneficial or worse (and it did not work!). My second thought was that everything works fine, and once I publish my app all will be good. But regardless, my logs are not working for the AdListener. If I can get that going, I think I can start verifying with more confidence of what is going on.
Thank you in advance for your help.
Log SS
回答1:
OK, several problems here.
1) No you are not using the AdListener interface correctly. Don't create your own interface. AdMob already supplies an AdListener interface. You do need to implement it however.
NB personally I would create an anonymous implementation of the AdListener instead of adding it to the Activity, but that's a design design for yourself.
Next you need to attach your listener to your AdView. So in your #onCreate
// load banner ads
adView.setAdListener(new AdListener() {
...
});
adView.loadAd(adRequestBanner);
2) You are unlikely to see any Interstitials with the code above because you are attempting to display immediately after requesting. It is extremely unlikely that an Interstitial has been downloaded in that short time frame. And in any case you really don't want to display an interstitial every time your Activity is created, it is a poor user experience.
Move your call to displayInterstitial() to some later point in your app life cycle, such at the end of a game session or between levels etc.
回答2:
Last I looked at admob the interstitial ads were invite only. Meaning they would contact you if they felt your apps were a good fit with that type of advertising.
来源:https://stackoverflow.com/questions/20862601/how-to-get-test-ad-banners-and-test-interstitial-ads-working-for-admob