[Android] Usage of Handler

handler first experience

Multi-threaded and asynchronous: multi-threaded development, asynchronous means don’t develop in the main thread

Android program startup law: Start the application----"Start Application-----"Start the process----"new a main thread in the process (UI thread, responsible for control + event distribution)----"Consumption When the operation is like a network request, create another thread to do it ---- "After others have done it, I should be notified, involving communication between threads

Handler can realize: communication between threads; cross-process communication (message is used)

  • Timed execution of Message and MessageQueue
  • Execute in different threads

Message、MessageQueue、Looper

Message: 2 integer values, 1 Object
MessageQueue: Message queue
Looper: message pump

Looper:

  • Manager of Message Queue

  • Looper.prepare() Each Looper object is associated with a thread

  • Looper.myLooper() can get the Looper object of the current thread

  • Looper takes out Message from MessageQueue

  • Hand over to Handler's handleMessage for processing

  • Call Message.recycle() to put it in the Message Pool

Handler:

Message message = mTestHandler.obtainMessage();

mTestHandler.sendMessageDelayed(message, 1000);

mTestHandler.postDelayed(new Runnable() {
    
    
            @Override
            public void run() {
    
    
                
            }
        },1000); // 延时1s执行

Countdown from 10 to 0 every 1s

Avoid memory leaks through weak references:

package com.jsc4.aboutactivity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.widget.HeaderViewListAdapter;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import java.lang.ref.WeakReference;

public class HandlerButtonActivity extends AppCompatActivity {
    
    
    private static final int MESSAGE_CODE = 888888;
    private TextView mTextView;
    private TestHandler mTestHandler = new TestHandler(this);

    public TextView getTextView() {
    
    
        return mTextView;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler_button);
        mTextView = findViewById(R.id.handler_text_view);
        
        // 发送消息
        Message message = mTestHandler.obtainMessage();
        message.arg1 = 0;
        message.arg2 = 1;
        message.what = MESSAGE_CODE;
        message.obj = 10000;
        mTestHandler.sendMessageDelayed(message, 1000);
    }


    public class TestHandler extends Handler{
    
    

        // 弱引用
        public final WeakReference<HandlerButtonActivity>  mHandlerButtonActivityWeakReference;
        public TestHandler(HandlerButtonActivity activity){
    
    
            mHandlerButtonActivityWeakReference = new WeakReference<HandlerButtonActivity>(activity);
        }

        @Override
        public void handleMessage(@NonNull Message msg) {
    
    
            super.handleMessage(msg);
            HandlerButtonActivity handlerButtonActivity = mHandlerButtonActivityWeakReference.get();

            // 接收消息
            switch (msg.what){
    
    
                case MESSAGE_CODE:
                    int value = (int) msg.obj;
                    handlerButtonActivity.getTextView().setText(String.valueOf(value/1000));

                    msg = Message.obtain();
                    msg.arg1 = 0;
                    msg.arg2 = 1;
                    msg.what = MESSAGE_CODE;
                    msg.obj = value - 1000;

                    if(value > 0){
    
    
                        sendMessageDelayed(msg, 1000);
                    }
                    break;


            }
        }
    }
}

Guess you like

Origin blog.csdn.net/qq_30885821/article/details/108814792