10

i've seen this thread : How to implement a listener about implement listeners.

its actually pretty simple but i don't get how exactly its done and how to implement in my own code.

i have this static variable variable: AppLoader.isInternetOn. i want to build a listener which will listen to this variable changes and update a TextView.

should i do this: ?

build an interface:

public interface InternetStateListener {
     public void onStateChange();
}

run it in my activity:

public class MyActivity extends Activity {
private InternetStateListener mListener;

setTheListener(this);

public void setTheListener(InternetStateListener listen) {
    mListener = listen;
}

private void onStateChange() {
    if (mListener != null) {
       if (AppLoader.isInternetOn)
        text.setText("on")
       else
        text.setText("off")
    }
  }
}

2 Answers 2

25

Your Activity does nothing special, just register itself (since the interface is implemented directly in the class) with the Other class that provides the listener.

public class MyActivity extends Activity implements InternetManager.Listener {

    private TextView mText;
    private InternetManager mInetMgr;

    /* called just like onCreate at some point in time */ 
    public void onStateChange(boolean state) {
        if (state) {
            mText.setText("on");
        } else {
            mText.setText("off");
        }
    }

    public void onCreate() {
        mInetMgr = new InternetManager();
        mInetMgr.registerListener(this);
        mInetMgr.doYourWork();
    }
}

The other class has to do pretty much all the work. Besides that it has to handle the registration of listeners it has to call the onStateChange method once something happend.

public class InternetManager {
    // all the listener stuff below
    public interface Listener {
        public void onStateChange(boolean state);
    }

    private Listener mListener = null;
    public void registerListener (Listener listener) {
        mListener = listener;
    }

    // -----------------------------
    // the part that this class does

    private boolean isInternetOn = false;
    public void doYourWork() {
        // do things here
        // at some point
        isInternetOn = true;
        // now notify if someone is interested.
        if (mListener != null)
            mListener.onStateChange(isInternetOn);
    }
}
3
  • ohhhh i think iv'e now realized the architecture! many thanks!
    – Asaf Nevo
    Mar 27, 2012 at 6:40
  • So for any classes inheriting the interface, they will be notified with onStateChange() when mListener.onStateChange(boolean) is called directly from the interface? So its like the interface is communicating with any sub inheriting classes whenever a variable changes in the main interface after completing a task that changes the variable, right? Thanks :) Jun 25, 2016 at 17:32
  • If so, how do I call doYourWork() from an activity? Jun 25, 2016 at 17:33
0

The part that you're missing it the class that actually notifies the listener. So you would need a class (most likely a service) that runs and pings the state of the network. Then when it detects a change it should call onStateChange() in any registered listeners. Then you would call setTheListener on that service, not on your activity.

Here's a link that thoroughly describes this design pattern: http://en.wikipedia.org/wiki/Observer_pattern

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.