Handler消息处理(零基础)

在程序开发时,对于一些耗时的操作,通常会为其开辟一个单独的线程来执行,减少用户的等待时间,在Android中,默认情况下,所有的操作都在主线程总进行,主线程负责管理与UI相关的事件,

Handler 是Android提供的一个用来更新UI的机制,也是一个消息处理的机制。通过Handle类可以发送和处理Message对象到其所在线程的MessageQueue中,

常用类中的常用方法

hangleMessage(Message msg)  处理消息的方法,通常重写该方法来处理消息,在发送消息,该方法自动回调
hasMessage(int what)检查消息队列是否包含what属性为指定值的消息
hasMessage(int what,Object object)检查消息队列是否包含what,object属性为指定值的消息

 

package com.example.testapplication;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.annotation.Nullable;

public class Handler3Activity extends ApplicationActivity{
    final int TIME = 60; // 定义时间长度
    final int TIMER_MSG = 0X001; // 定义消息代码
    private ProgressBar timer; // 声明水平进度条
    private int mProgressStatus = 0; // 定义完成进度
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler3);
        timer = (ProgressBar) findViewById(R.id.timer);
        handler.sendEmptyMessage(TIMER_MSG);
    }

    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg){
            // 当前进度大于0
            if(TIME - mProgressStatus > 0){
                mProgressStatus++;  // 进度+1
                timer.setProgress(TIME - mProgressStatus); // 更新进度条的显示进度
                handler.sendEmptyMessageDelayed(TIMER_MSG,1000); // 延迟一秒发送消息
            }else{
                // 提示时间已到
                Toast.makeText(Handler3Activity.this, "时间到!游戏结束!", Toast.LENGTH_SHORT).show();
            }
        }
    };
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/handle"
    >
    <ProgressBar
        android:id="@+id/timer"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="220dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="13dp"
        android:layout_marginLeft="10dp"
        android:max="60"
        ></ProgressBar>

</RelativeLayout>

 

 

package com.example.testapplication;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.TextView;

import androidx.annotation.Nullable;

import java.util.Timer;
import java.util.TimerTask;

public class Handler4Activity extends ApplicationActivity{
    private Timer timer; // 计时器
    private TextView textView; // 显示倒计时的文本控件
    private int i = 5; // 设置倒数的最大数
    public  static Handler4Activity instance = null; //定义静态的闪屏界面用于打开主界面后关闭该界面
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_handler4);
        // 全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        instance = this;  // 赋值本界面
        textView = (TextView) findViewById(R.id.text33);
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Message message = new Message();  // 初始化message
                message.what = 1;
                if(i >= 0){
                    handler.sendMessage(message);  // 发送消息给handler
                }
                if(i == 0){
                    cancel();   // 清除计时器
                }
            }
        },0,1000);  // 每1秒执行一次该方法

    }
    Handler handler = new Handler(new Handler.Callback(){
       @Override
       public boolean handleMessage(Message msg){
           if(msg.what == 1){
               i--; // 秒数倒数-1
               textView.setText("倒计时(" + i + ")");
               if(i == 0){
                   Intent intent = new Intent(Handler4Activity.this,Main2Activity.class);
                   startActivity(intent);
               }
           }
           return false;
       }
    });
}

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_height="match_parent"
    android:background="@mipmap/bj"
    tools:context="com.mingrisoft.flashscreen.MainActivity">

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="20dp"
    android:layout_marginEnd="20dp"
    android:layout_marginTop="14dp"
    android:textColor="#000000"/>

</RelativeLayout>

消息类(Message)

arg1(int)存放整型数据
arg2(int)存放整型数据
objObject类型对象
what(int)用于指定用户自定义消息代码,这样接收者可以了解这个消息的信息

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值