handler更新UI、消息处理

转载请注明出处:handler更新UI、消息处理_Mr_Leixiansheng的博客-CSDN博客

作用:

1、更新UI(子线程不能更新UI,通过发送消息到hanlder,在主线程更新UI)

2、消息处理(对发回来的消息进行相应处理)

3、弱引用,消除handler内存泄漏问题 (记得销毁是移除所有hanlder内容)

代码如下:

1、界面布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.leixiansheng.myhandler.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:text="Handle 更新UI/处理消息"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/text_view2"
        android:text="Message更新UI"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

2、功能实现

package com.example.leixiansheng.myhandler;

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

import java.lang.ref.WeakReference;

/**
 * handler 弱引用方式消除内存泄漏
 */
public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private TextView textView2;
    private ImageView imageView;
    private int[] images = {R.drawable.pic1, R.drawable.pic2, R.drawable.pic3};
    private int index = 0;

    private MyRunnable myRunnable = new MyRunnable();
    private MainHandler mainHandler;

    private static class MainHandler extends Handler {
        private WeakReference<MainActivity> weakReference;

        public MainHandler(MainActivity activity) {
            weakReference = new WeakReference<MainActivity>(activity); //MainActivity 弱引用
        }

        @Override
        public void handleMessage(Message msg) {
            MainActivity activity = weakReference.get(); //获取MainActivity弱引用对象
            Log.i("Handler", String.valueOf(msg.what));
            if (activity != null) {
               activity.textView2.setText("已完成Message方式跟新UI"); //获取MainActivity控件并进行相关操作
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = (TextView) findViewById(R.id.text_view);
        textView2 = (TextView) findViewById(R.id.text_view2);
        imageView = (ImageView) findViewById(R.id.image_view);

        mainHandler = new MainHandler(this);
        mainHandler.postDelayed(myRunnable, 1000);

        //在子线程用post更新UI
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(3000);
                    mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            textView.setText("Thread更新UI");
                        }
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        //在子线程中sendMessage更新UI
        new Thread() {
            @Override
            public void run() {
                super.run();
                mainHandler.sendEmptyMessageDelayed(1, 8000);
                //发送对象
//                mainHandler.sendMessage(new Message());
            }
        }.start();

    }

    //循环更新UI
    private class MyRunnable implements Runnable {

        @Override
        public void run() {
            index++;
            index = index % 3;
            imageView.setImageResource(images[index]);
            mainHandler.postDelayed(myRunnable, 1000);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mainHandler.removeCallbacksAndMessages(null); //清除所有Handler动作
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值