Android Getting Started tutorial | listener mode (use in Java and Android)

Listener mode (observer mode) can reduce the coupling between objects and decouple two interdependent calling classes.

Facilitate modular development. Developers of different modules can focus on their own code.

Listeners are used to listen to events of interest to themselves, and perform customized operations when receiving events of interest to themselves.
When some data changes, other classes respond. Classes that handle data (or distribute events) actively deliver messages, and interested classes actively "subscribe" to messages.

Listener mode is widely used in Android. I believe everyone will not feel strange. In Android development, the click event of Button control is the most common example of listener mode.

When the Button is clicked, OnClickListener onClick. The OnClickListener implemented by yourself is set for this Button in the Activity, and the onclick method is copied to perform custom operations.

Java code instance

Let's use Java to implement listener mode.
This example is to continuously pass in data to the "calculation class". After processing the data, send out the results. The class of interest receives the results.

2 files: algocalculator javaï¼›
* is the calculation part, which receives data and performs calculation. And pass the results* is the caller. It passes basic data into algocalculator and listens for the results.

package com.algo;

import java.util.LinkedList;
import java.util.List;

public class AlgoCalculator {

    private List<short[]> mDataBuffer = new LinkedList<>();

    public AlgoCalculator() {


    // Define a Listener interface; A boolean value can be passed out
    public interface ResultChangeListener {
        void onChange(boolean found);

    private ResultChangeListener resultChangeListener;
    // The caller can set and implement this interface
    public void setResultChangedListener(ResultChangeListener resultChangedListener) {
        this.resultChangeListener = resultChangedListener;
    // Transmit data
    public void setDataStream(short[] data) {
        checkData(data);// Data processing method

    // Process data and send results
    private void checkData(short[] data) {
        if (data.length == 0) {
        long sum = 0;
        for (short b : data) {
            sum += b;
        if (sum > 40) {
            resultChangeListener.onChange(true); // Data processing results
        } else {

Main program; The caller passes in data and gets the result

import com.algo.AlgoCalculator;

public class MainUser {
    public static void main(String[] args) {
        AlgoCalculator algoCalculator = new AlgoCalculator(); // initialization

        // Set up a listener and add actions to be performed in it
        algoCalculator.setResultChangedListener(new AlgoCalculator.ResultChangeListener() {
            public void onChange(boolean found) {
                System.out.println("result: " + found);
        short[] data1 = {1, 2, 3,};
        short[] data2 = {10, 20, 30};
        short[] data3 = {6, 7, 8};
        short[] data4 = {1, 1, 1};
        // incoming data 
        algoCalculator.setDataStream(data1);    // output false
        algoCalculator.setDataStream(data2);    // output true
        algoCalculator.setDataStream(data3);    // output false
        algoCalculator.setDataStream(data4);    // output false

In another class, it is convenient to call AlgoCalculator's computing power and obtain the calculation results. Here, each time data is passed in, a result can be obtained. If you pass in data every second, you can get a result every second. We can encapsulate complex algorithms. The client only needs to pass in data to obtain (listen to) results.

Listener mode is used in many scenarios. Developers may also unknowingly use this model.

Using listeners in Android

The most common example is to set a click event listener for a Button. Similar to the previous example, an interface is designed as a listener. During callback in Android, you can use handler to control the calling thread.

private Handler mMainHandler;

mMainHandler = new Handler(Looper.getMainLooper());// Run in main thread

private void notifySthChange(final int state) { Runnable() {
        public void run() {
            ArrayList<SListener> list = new ArrayList<>(mListeners);
            for (SListener l : list) {

The UI can be updated directly in the callback.

[Android zero foundation tutorial video reference]

Keywords: Android

Added by Brusca on Sun, 02 Jan 2022 20:36:27 +0200