[android] project application global unique Handler (interface callback Implementation)

Recently, the handler has been playing. I suddenly want to have a global handler. I meditate for 3 hours and finally get it done!

  1. First, create the Handler and the interface for callback in the Application, and print the address value of this interface (for later testing)

    //Application.class
    private static Handler mHandler = new Handler(Looper.getMainLooper()) {
       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
           mListener.heandleMessage(msg);
           System.out.println("mListener Address value==== " + mListener);
       }
    };
    
    private static HandlerListener mListener;
    public static void setOnHandlerListener(HandlerListener listener) {
       mListener = listener;
    }
    public  static HandlerListener getListener(){
       return mListener;
    }
    
    public  interface HandlerListener {
       void heandleMessage(Message msg);
    }
  2. In the Application, it is simulated to create a sub thread to send a message, which is usually sent after a network request to inform the Activity to modify the UI

    //Application.class
    @Override
    public void onCreate() {
        super.onCreate();
        mContext = this;
        new Thread() {
            @Override
            public void run() {
                super.run();
                for (int i = 0; i < 15; i++) {
                    try {
                        sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    mHandler.sendEmptyMessage(i);
                }
            }
        }.start();
    }
  3. Set this listening in MainActivity

    //MainActivity.class
        @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MyApplication.setOnHandlerListener(new MyApplication.HandlerListener() {
            @Override
            public void heandleMessage(Message msg) {
                System.out.println("MainActivity_msg==== " + msg.what);
            }
        });
    }
    
    //Add a jump button
    mButton = (Button) findViewById(R.id.button);
       mButton.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View view) {
           //Jump to HandlerActivity
               Intent intent = new Intent(MainActivity.this, HandlerActivity.class);
               startActivity(intent);
           }
       });
  4. Loop print the message that starts out, and the address value of the current mListener, the good play is about to start!

    System.out: MainActivity_msg==== 0
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$2@22078df
    System.out: MainActivity_msg==== 1
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$2@22078df
    System.out: MainActivity_msg==== 2
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$2@22078df
    System.out: MainActivity_msg==== 3
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$2@22078df
    System.out: MainActivity_msg==== 4
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$2@22078df
    System.out: MainActivity_msg==== 5
  5. Create another HandlerActivity and jump to HandlerActivity when the message is sent. What's the result?

    //HandlerActivity.class
    //HandlerActivity also sets a message listener
     mHandlerListener = new MyApplication.HandlerListener() {
            @Override
            public void heandleMessage(Message msg) {
                System.out.println("HandlerActivity_msg==== " + msg.what);
            }
        };
  6. It's obvious that the first monitor was covered, in fact
    Omit some log s, the address changes, and the Acticviy of the received message changes

    System.out: MainActivity_msg==== 4
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$1@54a87ea
    System.out: MainActivity_msg==== 5
    System.out: mListener Address value==== com.hdjz.mytest.activity.MainActivity$1@54a87ea
    System.out: HandlerActivity_msg==== 6
    System.out: mListener Address value==== com.hdjz.mytest.activity.HandlerActivity$2@aa3a08b
    System.out: HandlerActivity_msg==== 7
    System.out: mListener Address value==== com.hdjz.mytest.activity.HandlerActivity$2@aa3a08b
    System.out: HandlerActivity_msg==== 8
    System.out: mListener Address value==== com.hdjz.mytest.activity.HandlerActivity$2@aa3a08b
    System.out: HandlerActivity_msg==== 9
    System.out: mListener Address value==== com.hdjz.mytest.activity.HandlerActivity$2@aa3a08b
  7. When we return to the previous activity, the log continues to print the current activity data. The previous monitoring has been replaced. To this end, I can only say that if we lose it, we will lose it forever and never come back. Love!

  8. Note: the only handler in the whole project is message.what. Different message types have different functions. It's the only way to avoid the embarrassment of creating a handler for every activity

Keywords: network

Added by Drumminxx on Sat, 04 Apr 2020 04:04:22 +0300