AdMobs adListener

edited January 2014 in Android Mode

Hi everyone,

I use Admob and I would like to implement an adListener function on my application. The ads are actually correctly working. But after several test and modification, the code just can't work (there is a compiling error) with this line :
adView.setAdListener(this); Here is the documentation of this code. The questions are:
-does anyone has done this in the PDE before?
-If not, how could I do to implements the interface AdListener and use it with adView.setAdListener(AdListener adListener); ?
-Is there an other way to detect when a clic have been done on the adMob ad?

Thanks !

Answers

  • You are delving into some advanced Java concepts that it seems you aren't quite ready for yet. In short, the base class for all Processing sketches, PApplet does not implement AdListener (this is similar to extension for inheritance, but there are differences). When you call adView.setAdListener(this), AdMob expects this (your PApplet-based Processing sketch) to implement AdListener, which it does not. Although it is possible to make your sketch implement AdListener, it is probably easier to go the anonymous class route (this code taken from the link you provided...):

    adView.setAdListener(new AdListener() {
      @ Override
      public void onAdOpened() {
        //Your code here
      }
    });
    

    You may run into problems going down this route (such as "Variable x isn't final", or something like that). If this is the case, then you can create a separate listener class. For now, I'll leave this solution because it is more concise and easier to pull off than the other solution.

  • edited January 2014

    Thanks again calsign :) Unfortunately, the code you gave me make an error. I've also tried to make a separate adListener class:

    public interface AdListener{
        void onDismissScreen(Ad ad);
        void onLeaveApplication(Ad ad);
        void onPresentScreen(Ad ad);
        void onReceiveAd(Ad ad);
    }
    class listener implements AdListener{
        void onDismissScreen(Ad ad){
        }
        void onLeaveApplication(Ad ad){
        }
        void onPresentScreen(Ad ad){
        }
        void onReceiveAd(Ad ad){
        }
    }
    

    And the setAdListener inside of onCreate: adView.setAdListener(new listener());

    The first part can compile easily, but not the second. Am I missing something? Thanks !

  • Answer ✓

    This method is a bit more complicated. I'll try to spare you the gory details:

    //Global
    
    //Custom AdListener implementation
    class MyAdListener implements AdListener {
        void onAdOpened() {
            //Your code here
        }
    }
    
    //An instance of the custom AdListener implementation
    MyAdListener adListener;
    
    //In setup(), or wherever else you create the ad
    
    //Create the custom AdListener instance
    adListener = new MyAdListener();
    //Set the ad view to use the ad listener
    adView.setAdListener(adListener);
    

    This method should avoid errors about final variables. However, if this is not your error, then there is something else going wrong...

  • edited January 2014

    I'm afraid something else is going wrong :/

    And I just saw that I gave you the wrong 1st link, you should go on : "6.4.1 and earlier SDK" section.I guess it will help ( :-S ). Here is what I have:

    //global
    public interface AdListener{
        void onReceiveAd(Ad ad);
    
    }
    
    class MyAdListener implements AdListener {
        void onReceiveAd(Ad ad){
        }
    }
    MyAdListener adListener;
    
    
     //onCreate
     //custom instance
     adListener = new MyAdListener();
    

    Theses lines compile fine, contrary to this:

     //setAdListener method (also in onCreate)
     adView.setAdListener(adListener);
    

    Thanks for helping, calsign ^^

  • I must say I'm not a smart guy !

    I followed your recommendations with the 6.4.1 SDK section, and it worked ! here is what I wrote:

    class MyAdListener  implements AdListener {
        void onReceiveAd(Ad ad){
          println("receive");
        }
        void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode error){
          println("error");
        }
        void onPresentScreen(Ad ad){
          println("present");
        }
        void onDismissScreen(Ad ad){
          println("dismiss");
        }
        void onLeaveApplication(Ad ad){
          println("leave");
        }
    }
    MyAdListener adListener;
    

    The rest in onCreate is exactly the same. Thanks for all calsign, I owe you a lot :)

Sign In or Register to comment.