• 多线程和消息机制


     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.hanqi.testapp2.TestActivity6"
    11     android:orientation="vertical">
    12 
    13     <TextView
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:text="城市1"
    17         android:id="@+id/tv_3"/>
    18     <TextView
    19         android:layout_width="match_parent"
    20         android:layout_height="wrap_content"
    21         android:text="城市2"
    22         android:id="@+id/tv_4"/>
    23     <Button
    24         android:layout_width="match_parent"
    25         android:layout_height="wrap_content"
    26         android:text="启动"
    27         android:onClick="bt1_OnClick"/>
    28 
    29     <TextView
    30         android:layout_width="wrap_content"
    31         android:layout_height="wrap_content"
    32         android:text="接收的消息"
    33         android:id="@+id/tv_5"/>
    34 
    35     <Button
    36         android:layout_width="match_parent"
    37         android:layout_height="wrap_content"
    38         android:text="发消息"
    39         android:onClick="bt2_OnClick"/>
    40 
    41     <TextView
    42         android:layout_width="wrap_content"
    43         android:layout_height="wrap_content"
    44         android:text="10"
    45         android:id="@+id/tv_6"
    46         android:layout_gravity="center"/>
    47 
    48     <Button
    49         android:layout_width="match_parent"
    50         android:layout_height="wrap_content"
    51         android:text="增加"
    52         android:onClick="bt3_OnClick"
    53         android:id="@+id/bt_3"/>
    54 
    55     <Button
    56         android:layout_width="match_parent"
    57         android:layout_height="wrap_content"
    58         android:text="减少"
    59         android:onClick="bt3_OnClick"
    60         android:id="@+id/bt_4"/>
    61 
    62     <Button
    63         android:layout_width="match_parent"
    64         android:layout_height="wrap_content"
    65         android:text="暂停"
    66         android:onClick="bt3_OnClick"
    67         android:id="@+id/bt_5"
    68         android:enabled="false"/>
    69 
    70 </LinearLayout>
      1 package com.hanqi.testapp2;
      2 
      3 import android.os.Handler;
      4 import android.os.Message;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.os.Bundle;
      7 import android.view.View;
      8 import android.widget.Button;
      9 import android.widget.TextView;
     10 import android.widget.Toast;
     11 
     12 import java.util.Random;
     13 
     14 public class TestActivity6 extends AppCompatActivity {
     15 
     16     TextView tv_3, tv_4, tv_5,tv_6;
     17 
     18     Button bt_3, bt_4, bt_5;
     19 
     20     //定义Handler
     21     Handler h = new Handler(){
     22         @Override
     23         public void handleMessage(Message msg) {
     24             super.handleMessage(msg);
     25 
     26             //处理消息
     27             if (msg.what == 1)
     28             {
     29                 String m = msg.obj.toString();
     30 
     31                 tv_5.setText(tv_5.getText() + " " + m);
     32             }
     33             else if (msg.what == 2)
     34             {
     35                 tv_5.setText(tv_5.getText() + " 空消息");
     36             }
     37         }
     38     };
     39 
     40     int i = 10;
     41 
     42     Handler hl = new Handler()
     43     {
     44         @Override
     45         public void handleMessage(Message msg) {
     46             super.handleMessage(msg);
     47 
     48             switch (msg.what)
     49             {
     50                 case 1:
     51 
     52                     bt_3.setEnabled(false);
     53                     bt_4.setEnabled(true);
     54                     bt_5.setEnabled(true);
     55 
     56                     if (i == 20)
     57                     {
     58                         bt_5.setEnabled(false);
     59                         return;
     60                     }
     61 
     62                     i++;
     63 
     64                     tv_6.setText(i + "");
     65 
     66                     //发送
     67                     hl.sendEmptyMessageDelayed(1, 1000);
     68 
     69                     hl.removeMessages(2);
     70 
     71                     break;
     72 
     73                 case 2:
     74 
     75                     bt_3.setEnabled(true);
     76                     bt_4.setEnabled(false);
     77                     bt_5.setEnabled(true);
     78 
     79                     if (i == 1)
     80                     {
     81                         bt_5.setEnabled(false);
     82                         return;
     83                     }
     84 
     85                     i--;
     86 
     87                     tv_6.setText(i + "");
     88 
     89                     //发送
     90                     hl.sendEmptyMessageDelayed(2,1000);
     91 
     92                     hl.removeMessages(1);
     93 
     94                     break;
     95 
     96                 case 3:
     97 
     98                     bt_3.setEnabled(true);
     99                     bt_4.setEnabled(true);
    100                     bt_5.setEnabled(false);
    101 
    102                     hl.removeMessages(1);
    103                     hl.removeMessages(2);
    104 
    105                     break;
    106             }
    107         }
    108     };
    109 
    110     @Override
    111     protected void onCreate(Bundle savedInstanceState) {
    112         super.onCreate(savedInstanceState);
    113         setContentView(R.layout.activity_test6);
    114 
    115         tv_3 = (TextView)findViewById(R.id.tv_3);
    116         tv_4 = (TextView)findViewById(R.id.tv_4);
    117         tv_5 = (TextView)findViewById(R.id.tv_5);
    118         tv_6 = (TextView)findViewById(R.id.tv_6);
    119 
    120         bt_3 = (Button)findViewById(R.id.bt_3);
    121         bt_4 = (Button)findViewById(R.id.bt_4);
    122         bt_5 = (Button)findViewById(R.id.bt_5);
    123     }
    124 
    125 
    126     //3个按钮
    127     public void bt3_OnClick(View v)
    128     {
    129         switch (v.getId())
    130         {
    131             case R.id.bt_3:
    132 
    133                 //发送增加消息
    134                 hl.sendEmptyMessage(1);
    135 
    136                 break;
    137 
    138             case R.id.bt_4:
    139 
    140                 //发送减少消息
    141                 hl.sendEmptyMessage(2);
    142 
    143                 break;
    144 
    145             case R.id.bt_5:
    146 
    147                 //发送暂停消息
    148                 hl.sendEmptyMessage(3);
    149 
    150                 break;
    151         }
    152     }
    153 
    154 
    155     //发消息
    156     public void bt2_OnClick(View v)
    157     {
    158         //启动线程
    159         new Thread(){
    160             @Override
    161             public void run() {
    162 
    163                 //发送消息
    164                 //1、创建Message
    165                 Message m = Message.obtain();
    166                 m.what = 1;//id
    167                 m.obj = "新信息1";
    168 
    169                 //2、发送即时消息
    170                 h.sendMessage(m);
    171 
    172                 m = Message.obtain();
    173                 m.what = 1;//id
    174                 m.obj = "新信息2";
    175 
    176                 //发送延时消息
    177                 h.sendMessageDelayed(m,2000);
    178 
    179                 h.sendEmptyMessage(2);
    180 
    181                 h.sendEmptyMessageDelayed(2,3000);
    182             }
    183         }.start();
    184     }
    185 
    186 
    187     public void bt1_OnClick(View v)
    188     {
    189         tv_3.setText("");
    190         tv_4.setText("");
    191 
    192         //创建子线程1
    193         new Thread(){
    194             @Override
    195             public void run() {
    196 
    197                 for (int i = 0; i<10; i++) {
    198                     try {
    199                         //暂停时间随机
    200                         Thread.sleep(new Random().nextInt(2000));
    201                     }
    202                     catch (Exception e)
    203                     {
    204 
    205                     }
    206 
    207                     runOnUiThread(new Runnable() {
    208                         @Override
    209                         public void run() {
    210 
    211                           tv_3.setText(tv_3.getText() + "北京");
    212                         }
    213                     });
    214 
    215                 }
    216 
    217                 runOnUiThread(new Runnable() {
    218                     @Override
    219                     public void run() {
    220                         Toast.makeText(TestActivity6.this, "北京 循环完成", Toast.LENGTH_SHORT).show();
    221                     }
    222                 });
    223             }
    224         }.start();
    225 
    226 
    227 
    228         //创建子线程2
    229         new Thread(){
    230             @Override
    231             public void run() {
    232 
    233                 for (int i = 0; i<10; i++) {
    234                     try {
    235                         //暂停时间随机
    236                         Thread.sleep(new Random().nextInt(2000));
    237                     }
    238                     catch (Exception e)
    239                     {
    240 
    241                     }
    242 
    243                     runOnUiThread(new Runnable() {
    244                         @Override
    245                         public void run() {
    246 
    247                             tv_4.setText(tv_4.getText() + "香港");
    248                         }
    249                     });
    250 
    251                 }
    252 
    253                 runOnUiThread(new Runnable() {
    254                     @Override
    255                     public void run() {
    256                         Toast.makeText(TestActivity6.this, "香港 循环完成", Toast.LENGTH_SHORT).show();
    257                     }
    258                 });
    259             }
    260         }.start();
    261     }
    262 }

  • 相关阅读:
    【Codeforces 475F】—Meta-universe(Set)
    【Codeforces 475F】—Meta-universe(Set)
    【UOJ #79】—一般图最大匹配(带花树)
    【UOJ #79】—一般图最大匹配(带花树)
    【NOIp2019模拟】题解
    【NOIp2019模拟】题解
    涉及到的业务逻辑总结
    2016-9-14
    Spring MVC
    2016-9-12
  • 原文地址:https://www.cnblogs.com/future-zhenzhen/p/5504190.html
Copyright © 2020-2023  润新知