Tuesday, February 14, 2017

Java Interface callback example

***** Interface class
public class ExtServiceCallback {
    static ExtServiceCallback.Callback sCallbacks = null;

    public void runServiceCallback(String path, String method, String query, HashMap<String, String> headers, String msgBody) {
        sCallbacks.downToExtService(path, method, query, headers, msgBody);
    }

    public void setCallback(ExtServiceCallback.Callback callback) {
        sCallbacks = callback;
    }

    public interface Callback {
        public void downToExtService(String path, String method, String query, HashMap<String, String> headers, String msgBody);
    }
}


***** App usage

protected void onCreate(Bundle savedInstanceState) {
    ServerCallback callback  = new ServerCallback();
}

public class ServerCallback extends ExtServiceCallback implements ExtServiceCallback.Callback {
    private static final String TAG = "ServerCallback";

    public ServerCallback() {
        Log.e(TAG, "ServerCallback");
        setCallback(this);
    }

    @Override    public void downToExtService(String path, String method, String query, HashMap<String, String> headers, String msgBody) {
        Log.e(TAG, "path: " + path + ", " +
                "method: " + method + ", " +
                "query: " + query + ", " +
                "msgBody: " + msgBody);
    }
}

No comments:

Post a Comment