欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Handler常见发送消息方法

程序员文章站 2022-07-14 16:46:16
...

代码实现最简单的Handler

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    tools:context="com.administrator.handlerproject.MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text"/>
   <Button
       android:id="@+id/btn1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="按钮"/>

</LinearLayout>

MainActivity.java

package com.administrator.handlerproject;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    /**
     * UI线程
     */
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = findViewById(R.id.textView);
        //创建一个Handler
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                if (msg.what==1001)//得到这个消息标记开始处理消息
//                //处理消息
                    textView.setText("weimei");
//                Log.d(TAG,"handleMessage:" + msg.what);
            }
        };
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //有可能做大量耗时操作
                /**
                 * 子线程
                 */
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(1001);//向消息队列发送一个标记
                    }
                }).start();
            }
        });
    }
}

----------------------------------------------

Handler的一些常用标记键,以及延时方法。

xml布局同上不变

MainActivity.java


package com.administrator.handlerproject;

import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    /**
     * UI线程
     */
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView = findViewById(R.id.textView);
        //创建一个Handler
        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {//接收到完整的包裹
                super.handleMessage(msg);
                if (msg.what==1002)//拆解包裹拿值
//                //处理消息
                    textView.setText("weimei");
                Log.d(TAG,"handleMessage:" + msg.arg1);

            }
        };
        findViewById(R.id.btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //有可能做大量耗时操作
                /**
                 * 子线程
                 */
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(1001);
                        Message message = Message.obtain();
                        message.what=1002;
                        message.arg1=1003;
                        message.arg2=1004;//将每个消息组装
                        message.obj=MainActivity.this;//打包发出去
                        handler.sendMessage(message);
                        /**
                         * 定时任务
                         */
                        handler.sendMessageAtTime(message, SystemClock.uptimeMillis()+3000);//计时,SystemClock.uptimeMillis()获取当前时间
                        handler.sendMessageDelayed(message,2000);//上面的是绝对时间,下面的是相对时间,即消息送走时间

                        final Runnable runnable = new Runnable() {
                            @Override
                            public void run() {
                                int a = 1 + 2 + 3;
                            }
                        };
                        handler.post(runnable);
                        runnable.run();

                        handler.postDelayed(runnable,2000);//延迟两秒
                        }
                    
                }).start();
            }
        });
    }
}