Search code examples
javaandroidlistenerimplements

How or why do listener interfaces work? And do interfaces have any other use rather than being a listener?


Whenever we want to create a listener, we implement a listener interface. For example, lets implement SensorEventListener.

Now we have to override the methods of this listener interface.

public void onSensorChanged(SensorEvent event);

and

public void onAccuracyChanged(Sensor sensor, int accuracy);

What I don't understand is:

  1. Why and how these methods work when I automatically use them?
  2. Why does onAccuracyChanged method gets called when the accuracy changes?
  3. After all, onAccuracyChanged is just an empty method that we override because our formula (or the interface we implement) requires us to do so. If it is something magical caused by the lower levels
  4. When and why would someone actually use an interface in his/her self-project regardless of android?

Solution

  • In computing, an interface is a shared boundary across which two or more separate components of a computer system exchange information.(Wikipedia)

    You may wish to respond to some events either system events or user events. But for that you need to know when the event you wish to capture occurs and also what must be done at that time.

    And for that you open a confidential EAR to listen to events. But that will not be sufficient since you need to be notified too so that you can reply according to the event. You set callbacks that will notify when an event occur. Those empty body methods we create inside an interface.

    A Listener is that interface that hears and notify back through callbacks.

    So how can all that be used? And how all these do interact?

    • First create an interface with empty bodies methods that you intend to call when an event occurs:
    public interface MyListener{
    
          void actionOneHappens(Object o);
          void actionTwo();
          void actionThree();
    
    }
    
    • Create a class that handles something, for example counts:
    public class MyCounter{
    //create a member of type MyListener if you intend to exchange infos
    
    private MyListener myListener;
    
    //let's create a setter for our listener
    public void setMyListener(MyListener listener)
    {
    this.myListener=listener;
    }
    
      MyCounter(){
    
      }
    //this method will help us count
    public void startCounting()
    {
      new CountDownTimer(10000,1000)
           {
    
               @Override
               public void onTick(long millisUntilFinished) {
    
                //I want to notify at third second after counter launched
    
                if(millisUntilFinished/1000==3)
                {
                  // I notify if true :
                  //as someone can forget to set the listener let's test if it's not //null
                  if(myListener!=null){
                     myListener.actionThree();
                  }
    
    
                }
    
               }
    
               @Override
               public void onFinish() {
    
               }
           }.start();
    }
    
    
    
    
    }
    
    • You can then create an object of type MyCounter and know when it's at three:

    MyCounter myCounter=new MyCounter();
    
    myCounter.setMyListener(new MyListener()
    {
    //then override methods here
      @override
      void actionOneHappens(Object o){
      }
      @override
      void actionTwo()
      {}
    
      @override
      void actionThree()
      {
       //Add you code here
       Toast.makeText(getApplicationContext(),"I'm at 3",Toast.LENGTH_LONG).show()
       }
    
    
    
    });
    
    //start your counter
    myCounter.startCounting();

    And it's done!! That's how we proceed.