面试常客Handler详细解析(handler基础)(一)

本文详细解析了Android中的Handler机制,包括其用途、如何使用、更新UI的方法、传递复杂数据、消息的移除、消息的截获等核心内容,并通过实例代码展示其实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Handler是什么?
handler是Android给我们提供用来更新UI的一套机制,也是一套消息处理的机制,我们可以用它发送消息,也可以通过它处理消息。
联系framework可以详细看到。生命周期的改变都是通过handler消息改变的。

为什么要用Handler?
不用handler更新UI是不行的,Android在设计的时候,就封装了一套消息创建、传递、处理机制,如果不遵循这样的机制,就没有办法更新UI信息的,就会抛出异常信息(不能在非UI线程中更新UI)。

子线程真的不能更新UI吗?
答案是否定的,下面看贴下自己已经运行通过的代码,用事实例证:
MainActivity.java:

package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById(R.id.tv);

        new Thread(){
            @Override
            public void run() {
                tv.setText("hi");
            }
        }.start();;

    }
}

对应的activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</RelativeLayout>

运行结果:
这里写图片描述
事实证明,子线程是可以更新UI的,但是如果子线程先休眠一段时间再去更新就会失败,这个还得从ViewRootImp的创建说起,ViewRootImp是在onResume()中创建的,没有休眠的子线程不会经历onResume()阶段,只有休眠过的子线程才会经历onResume()阶段,才会创建ViewRootImp,才会不能让子线程更新UI。但是我们不建议这么做,我们最好不要在子线程中更新UI。

Handler怎么用?
handler更新UI:
既然不能在子线程中更新UI,那我们就只能使用handler来更新UI了,下面配合代码实现:
MainActivity.java:

package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv;
    private Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv=(TextView) findViewById(R.id.tv);

        new Thread(){
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
//                  tv.setText("hi");
                    handler.post(new Runnable() {

                        @Override
                        public void run() {
                            tv.setText("update...");

                        }
                    });
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        }.start();

    }


}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</RelativeLayout>

通过runnable轮换显示三张照片:
MainActivity.java:

package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;
private Handler handler = new Handler();
private ImageView imageView;
private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
private int index = 0;

private MyRunnable  runnable =  new MyRunnable();

class MyRunnable implements Runnable{
    @Override
    public void run() {
        index++;
        index=index%3;
        imageView.setImageResource(images[index]);
        handler.postDelayed(runnable, 1000);

    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.tv);
    imageView = (ImageView) findViewById(R.id.imageView1);

    new Thread(runnable).start();//这两句话的作用是一样的
//  handler.postDelayed(runnable, 1000);//这两句话的作用是一样的,用哪句都一样

// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText(“hi”);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText(“update…”);
//
// }
// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
// }.start();

}

}

对应的activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="25dp"
        android:text="hello" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="164dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

发送处理消息:
MainActivity.java:

package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private TextView tv;
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            tv.setText(""+msg.arg1);
        };
    };
    private ImageView imageView;
    private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
    private int index = 0;

    private MyRunnable  runnable =  new MyRunnable();

    class MyRunnable implements Runnable{
        @Override
        public void run() {
            index++;
            index=index%3;
            imageView.setImageResource(images[index]);
            handler.postDelayed(runnable, 1000);

        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);
        imageView = (ImageView) findViewById(R.id.imageView1);

        new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                try {
                    Thread.sleep(2000);
                    Message message = new Message();
                    message.arg1 = 88;
                    handler.sendMessage(message);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }.start();
//       
//      new Thread(runnable).start();



//      new Thread(){
//          @Override
//          public void run() {
//              try {
//                  Thread.sleep(1000);
////                    tv.setText("hi");
//                  handler.post(new Runnable() {
//                      
//                      @Override
//                      public void run() {
//                          tv.setText("update...");
//                          
//                      }
//                  });
//              } catch (InterruptedException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
//              
//          }
//      }.start();

    }


}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="25dp"
        />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="164dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

传递复杂数据,比如自定义类,通过obj传递:
MainActivity.java:
package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

private TextView tv;
private Handler handler = new Handler(){
    public void handleMessage(android.os.Message msg) {
        tv.setText(""+msg.obj);
    };
};
private ImageView imageView;
private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
private int index = 0;

private MyRunnable  runnable =  new MyRunnable();

class MyRunnable implements Runnable{
    @Override
    public void run() {
        index++;
        index=index%3;
        imageView.setImageResource(images[index]);
        handler.postDelayed(runnable, 1000);

    }
}

class Person{
    private int age;
    private String name;
    @Override
    public String toString() {
        // TODO Auto-generated method stub
        return "name="+name+" age="+age;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.tv);
    imageView = (ImageView) findViewById(R.id.imageView1);

    new Thread(){
        @Override
        public void run() {
            // TODO Auto-generated method stub
            super.run();
            try {
                Thread.sleep(2000);
                Message message = new Message();
                message.arg1 = 88;
                message.arg2 = 100;
                Person person = new Person();
                person.age = 23;
                person.name = "Joe";
                message.obj= person;
                handler.sendMessage(message);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }.start();

//
// handler.postDelayed(runnable, 1000);

// new Thread(){
// @Override
// public void run() {
// try {
// Thread.sleep(1000);
//// tv.setText(“hi”);
// handler.post(new Runnable() {
//
// @Override
// public void run() {
// tv.setText(“update…”);
//
// }
// });
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// }
// }.start();

}

}
对应的布局文件activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="25dp"
        />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="164dp"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

消息的移除,当button被按下后,轮询图片的runnable被移除:

MainActivity.java:
package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

    private TextView tv;
    private Button button;
    private Handler handler = new Handler(){
        public void handleMessage(android.os.Message msg) {
            tv.setText(""+msg.obj);
        };
    };

    private ImageView imageView;
    private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
    private int index = 0;

    private MyRunnable  runnable =  new MyRunnable();

    //轮询显示三张图片
    class MyRunnable implements Runnable{
        @Override
        public void run() {
            index++;
            index=index%3;
            imageView.setImageResource(images[index]);
            handler.postDelayed(runnable, 1000);

        }
    }
    //自定义类,传递复杂数据类型使用
    class Person{
        private int age;
        private String name;
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "name="+name+" age="+age;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
        tv = (TextView) findViewById(R.id.tv);
        imageView = (ImageView) findViewById(R.id.imageView1);

        new Thread(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                super.run();
                try {
                    Thread.sleep(2000);
//                  Message message = new Message();
//                  message.arg1 = 88;
//                  message.arg2 = 100;
                    Message message = handler.obtainMessage();
                    Person person = new Person();
                    person.age = 23;
                    person.name = "Joe";
                    message.obj= person;
//                  handler.sendMessage(message);
                    message.sendToTarget();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }.start();
//       
        handler.postDelayed(runnable, 1000);



//      new Thread(){
//          @Override
//          public void run() {
//              try {
//                  Thread.sleep(1000);
////                    tv.setText("hi");
//                  handler.post(new Runnable() {
//                      
//                      @Override
//                      public void run() {
//                          tv.setText("update...");
//                          
//                      }
//                  });
//              } catch (InterruptedException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
//              
//          }
//      }.start();

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        handler.removeCallbacks(runnable);

    }


}

对应的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="25dp"
        />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="164dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/imageView1"
        android:text="Button" />

</RelativeLayout>

消息的截获:
通过生成handler时,带有一个callback接口,如果返回false,则不截获数据,如果返回true,则将数据截获,看下面的代码,查看按钮按下的toast信息就知道了:
MainActivity.java:

package com.example.handler;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    private TextView tv;
    private Button button;
    private Handler handler = new Handler(new Callback() {

        @Override
        public boolean handleMessage(Message msg) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), ""+1, 1).show();
            return true;
        }
    }){
        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            Toast.makeText(getApplicationContext(), ""+2, 1).show();
        }
    };

    private ImageView imageView;
    private int[] images = {R.drawable.a,R.drawable.b,R.drawable.c};
    private int index = 0;

    private MyRunnable  runnable =  new MyRunnable();

    //轮询显示三张图片
    class MyRunnable implements Runnable{
        @Override
        public void run() {
            index++;
            index=index%3;
            imageView.setImageResource(images[index]);
            handler.postDelayed(runnable, 1000);

        }
    }
    //自定义类,传递复杂数据类型使用
    class Person{
        private int age;
        private String name;
        @Override
        public String toString() {
            // TODO Auto-generated method stub
            return "name="+name+" age="+age;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(this);
        tv = (TextView) findViewById(R.id.tv);
        imageView = (ImageView) findViewById(R.id.imageView1);

//      new Thread(){
//          @Override
//          public void run() {
//              // TODO Auto-generated method stub
//              super.run();
//              try {
//                  Thread.sleep(2000);
////                    Message message = new Message();
////                    message.arg1 = 88;
////                    message.arg2 = 100;
//                  Message message = handler.obtainMessage();
//                  Person person = new Person();
//                  person.age = 23;
//                  person.name = "Joe";
//                  message.obj= person;
////                    handler.sendMessage(message);
//                  message.sendToTarget();
//              } catch (InterruptedException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
//          }
//          
//      }.start();
//       
//      handler.postDelayed(runnable, 1000);



//      new Thread(){
//          @Override
//          public void run() {
//              try {
//                  Thread.sleep(1000);
////                    tv.setText("hi");
//                  handler.post(new Runnable() {
//                      
//                      @Override
//                      public void run() {
//                          tv.setText("update...");
//                          
//                      }
//                  });
//              } catch (InterruptedException e) {
//                  // TODO Auto-generated catch block
//                  e.printStackTrace();
//              }
//              
//          }
//      }.start();

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
//      handler.removeCallbacks(runnable);
        handler.sendEmptyMessage(1);

    }


}

对应的布局文件activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.handler.MainActivity" >

    <TextView
        android:id="@+id/tv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/imageView1"
        android:layout_below="@+id/imageView1"
        android:layout_marginTop="25dp"
        />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="164dp"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignRight="@+id/imageView1"
        android:text="Button" />

</RelativeLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值