Android Handle详解
效果图
一.Handle
Handler 为Android操作系统中的线程通信工具,它主要由两个作用:
(1)安排消息或Runnable 在某个主线程中某个地方执行。
(2)安排一个动作在另外的线程中执行。
每个Handler对象维护两个队列(FIFO),消息队列和Thread队列, 都是有Android操作系统提供的。Handler可以通过这两个队列来分别:
发送、接受、处理消息–消息队列;
启动、结束、休眠线程–Thread队列;Handler的使用方法大体分为3个步骤:
1.创建Handler对象。
2.创建Thread和消息。
3.调用GET以及sendMessage方法将Thread和消息添加到队列。
Handler:
作用:用来满足线程与线程之间的通信
线程一旦被创建就会生成一个Looper对象
每个应用在运行的时候都会生成一个主线程(UI线程)
子线程不能做更新UI的操作,必须要在主线程中完成
Handler:通过Handler对象和Looper进行沟通,以便发送消息到MessageQueue或者接收Looper从MessageQueue中读取的消息
二.代码演示
MainActivity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private TextView mTextView; private Button mButton1, mButton2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控件 initView(); } private void initView() { mTextView = (TextView) findViewById(R.id.textView); mButton1 = (Button) findViewById(R.id.button1); mButton2 = (Button) findViewById(R.id.button2); mButton1.setOnClickListener(this); mButton2.setOnClickListener(this); } //创建一个Handle private Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { String string = null; //判断:根据Message的标识判断消息 if (msg.what == 0) { string = (String) msg.obj; } else if (msg.what == 1) { Bundle bundle = msg.getData(); string = bundle.getString("bundle"); } mTextView.setText(string); } }; @Override public void onClick(View v) { switch (v.getId()) { case R.id.button1: new Thread() { @Override public void run() { //可以让Message不回收,重复利用 Message msg = Message.obtain(); //给msg添加标识 msg.what = 0; //使用obj给msg传值 msg.obj = "按钮一"; //设置启动时间 mHandler.sendMessageDelayed(msg, 1000); } }.start(); break; case R.id.button2: new Thread() { @Override public void run() { //相当于New Message, Message msg = Message.obtain(); //给msg添加标识 msg.what = 1; //创建Bundle对象 Bundle bundle = new Bundle(); //存放字符串键值对 bundle.putString("bundle", "按钮二"); //给obtain添加bundle对象 msg.setData(bundle); //向消息池中发送消息 mHandler.sendMessage(msg); } }.start(); break; } } }
XML布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.httpurlconnection.MainActivity"> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView" android:layout_centerHorizontal="true" android:layout_marginTop="55dp" android:text="按钮一"/> <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignStart="@+id/button1" android:layout_below="@+id/button1" android:text="按钮二"/> <TextView android:id="@+id/textView" android:textSize="30sp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/button1" android:layout_alignParentTop="true" android:layout_alignStart="@+id/button1" android:text="Hello World!"/> </RelativeLayout>