zoukankan      html  css  js  c++  java
  • Handler实现线程间的通信1

    通过Handler实现线程间的通信,在主线程当中实现Handler的handlerMessage()方法,在WorkerThread中通过Handler发送消息

    Handler实现线程间的通信实例:

        实现点击发送消息,启动一个线程,然后让线程休眠2秒钟,把TextView中的内容修改掉
        这个例子的实际价值:当点击按钮的时候,程序假设去访问服务器,服务器接收到请求之后返回结果,假设这个结果是个字符串,然后把字符串更新到TextView中

    MainActivity.java

     1 import android.app.Activity;
     2 import android.os.Bundle;
     3 import android.os.Handler;
     4 import android.os.Message;
     5 import android.view.View;
     6 import android.view.View.OnClickListener;
     7 import android.widget.Button;
     8 import android.widget.TextView;
     9 
    10 public class MainActivity extends Activity {
    11     private TextView textView;
    12     private Button button;
    13     private Handler handler;
    14     protected void onCreate(Bundle savedInstanceState) {
    15         super.onCreate(savedInstanceState);
    16         setContentView(R.layout.activity_main);
    17         
    18         textView = (TextView) findViewById(R.id.textViewId);
    19         button = (Button) findViewById(R.id.buttonId);
    20         
    21         handler = new MyHandler();
    22         
    23         button.setOnClickListener(new ButtonListener());
    24     }
    25     
    26     //程序运行路线第三步:然后调用handler对象的handleMessage方法把消息对象传进来,然后执行这个方法中的内容
    27     class MyHandler extends Handler{
    28         public void handleMessage(Message msg){
    29             
    30             System.out.println("handleMessage---->"+Thread.currentThread().getName());
    31             
    32             String s = (String)msg.obj;
    33             
    34             //在主线程中就可以随意的操作textView(UI)了
    35             textView.setText(s);
    36         }
    37     }
    38     
    39     //程序运行路线第一步:当点击这个Button按钮的时候执行Oclick方法,生成了一个新的线程对象,然后让线程运行,
    40     class ButtonListener implements OnClickListener{
    41         @Override
    42         public void onClick(View v) {
    43             Thread t = new NetworkThread();
    44             t.start();
    45         }
    46         
    47     }
    48     
    49     //程序运行路线第二步:执行输出代码,然后使用handler的obtainMessage()方法获取了一个Message对象,然后使用handler调用sendMessage方法将消息对象发送到消息队列中,Looper将消息对象取出来
    50     class NetworkThread extends Thread{
    51         public void run(){
    52             
    53             System.out.println("network---->"+Thread.currentThread().getName());
    54             
    55             try {
    56                 Thread.sleep(2*1000);//模拟访问网络,所以当线程运行时,首先休眠2秒钟
    57             } catch (InterruptedException e) {
    58                 e.printStackTrace();
    59             }
    60             //把变量s的值,模拟从网络当中获取的数据
    61             String s = "从网络当中获取的数据";
    62             //textView.setTag(s);这样做不行,因为在Android系统当中,只有Main线程才可以操作UI,WorkerThread线程不可以操作UI,这样操作会报异常
    63             
    64             Message msg = handler.obtainMessage();
    65             msg.obj = s;//将s变量赋值给msg,然后发送出去,注意这里是obj属性接受的是Object类型的数据,what属性接受的是int类型
    66             
    67             handler.sendMessage(msg);//sendMessage()方法,无论是在主线程中发送还是在WorkerThread线程中发送都是可以的,一样可以被handleMessage()方法接收
    68         }
    69     }
    70 
    71 
    72 }

     activity_main.xml

     1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     android:paddingBottom="@dimen/activity_vertical_margin"
     6     android:paddingLeft="@dimen/activity_horizontal_margin"
     7     android:paddingRight="@dimen/activity_horizontal_margin"
     8     android:paddingTop="@dimen/activity_vertical_margin"
     9     tools:context=".MainActivity" >
    10 
    11     <TextView 
    12         android:id="@+id/textViewId"
    13         android:layout_width="match_parent"
    14         android:layout_height="wrap_content"
    15         android:text="数据"
    16         
    17         />
    18     
    19     <Button 
    20         android:id="@+id/buttonId"
    21         android:layout_width="match_parent"
    22         android:layout_height="wrap_content"
    23         android:layout_below="@id/textViewId"
    24         android:text="发送消息"
    25         />
    26 
    27 </RelativeLayout>
  • 相关阅读:
    【STSRM12】整除
    【STSRM12】夏令营
    【BZOJ】1954: Pku3764 The xor-longest Path
    Faster-rcnn实现目标检测
    反向传播算法的理解
    人工智能发展的看法
    FCN的理解
    卷积神经网络的部分知识理解
    不常见算法的介绍
    如何对模型进行优化
  • 原文地址:https://www.cnblogs.com/LO-ME/p/3587177.html
Copyright © 2011-2022 走看看