• ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)


    一、流程

    1.点击按钮,则代码会使handler把updateThread压到队列里去,从而执行updateThread的run()

    2.run()里会通过msg.arg1 = i 和bundle来写参,通过执行updateBarHandler.sendMessage(msg)把消息压入消息队列,会触发handler的handleMessage(Message msg)

    3.在handleMessage(Message msg)里,会获取参数更新进度条,再执行updateBarHandler.post(updateThread),再启动线程run方法,从而形成循环

    注:此例子的updateBarHandler.post(updateThread);是相当于直接调用线程的run(),所以activity与updateThread是处于同一个线程

    二、代码

    1.xml
    (1)main.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:orientation="vertical"
     4     android:layout_width="fill_parent"
     5     android:layout_height="fill_parent"
     6     >
     7 
     8 <ProgressBar
     9     android:id="@+id/bar"
    10     style="?android:attr/progressBarStyleHorizontal"
    11     android:layout_width="200dp"
    12     android:layout_height="wrap_content"
    13     android:visibility="gone"
    14     />
    15 <Button 
    16     android:id="@+id/startButton"
    17     android:layout_width="fill_parent" 
    18     android:layout_height="wrap_content" 
    19     android:text="start"/>
    20 </LinearLayout>

    2.java
    (1)TestBarHandler.java

     1 package mars.barhandler;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.os.Handler;
     6 import android.os.Message;
     7 import android.view.View;
     8 import android.view.View.OnClickListener;
     9 import android.widget.Button;
    10 import android.widget.ProgressBar;
    11 
    12 public class TestBarHandler extends Activity {
    13     /** Called when the activity is first created. */
    14     //声明控件变量
    15     ProgressBar bar = null;
    16     Button startButton = null;
    17     @Override
    18     public void onCreate(Bundle savedInstanceState) {
    19         super.onCreate(savedInstanceState);
    20         setContentView(R.layout.main);
    21         //根据控件的ID得到代表控件的对象,并为按钮设置监听器
    22         bar = (ProgressBar)findViewById(R.id.bar);
    23         startButton = (Button)findViewById(R.id.startButton);
    24         startButton.setOnClickListener(new ButtonListener());
    25     }
    26     //当点击startButton按钮时,就会执行ButtonListener的onClick方法
    27     class ButtonListener implements OnClickListener{
    28 
    29         @Override
    30         public void onClick(View v) {
    31             // TODO Auto-generated method stub
    32             bar.setVisibility(View.VISIBLE);
    33             updateBarHandler.post(updateThread);
    34         }
    35         
    36     }
    37     //使用匿名内部类来复写Handler当中的handleMessage方法
    38     Handler updateBarHandler = new Handler(){
    39 
    40         @Override
    41         public void handleMessage(Message msg) {
    42             bar.setProgress(msg.arg1);
    43             Bundle bundle = msg.getData();
    44             updateBarHandler.post(updateThread);
    45             System.out.println("test---->" + bundle.getString("test"));
    46         }
    47         
    48     };
    49     //线程类,该类使用匿名内部类的方式进行声明
    50     Runnable updateThread = new Runnable(){
    51         int i = 0 ;
    52         @Override
    53         public void run() {
    54             System.out.println("Begin Thread" + i);
    55             i = i + 10 ;
    56             //得到一个消息对象,Message类是有Android操作系统提供
    57             Message msg = updateBarHandler.obtainMessage();
    58             
    59             //将msg对象的arg1参数的值设置为i,用arg1和arg2这两个成员变量传递消息,优点是系统性能消耗较少
    60             msg.arg1 = i ;
    61             Bundle bundle = new Bundle();
    62             bundle.putString("test", "test bundle");
    63             msg.setData(bundle);
    64             try {
    65                 //设置当前显示睡眠1秒
    66                 Thread.sleep(1000);
    67             } catch (InterruptedException e) {
    68                 // TODO Auto-generated catch block
    69                 e.printStackTrace();
    70             }
    71             //将msg对象加入到消息队列当中
    72             if( i > 100){
    73                 //如果当i的值为100时,就将线程对象从handler当中移除
    74                 updateBarHandler.removeCallbacks(updateThread);
    75                 System.out.println(">>>>>>");
    76             }else{
    77                 //把消息压入消息队列,会触发handler的handleMessage方法
    78                 updateBarHandler.sendMessage(msg);
    79                 System.out.println("<<<<<<");
    80             }
    81         }
    82     };
    83     class MyThread extends Thread{
    84         public void run(){
    85             
    86         }
    87     }
    88     
    89 }

    ps:为什么线程里的int i = 0 ;为什么不会每执行一次线程i就归0?

    
    
  • 相关阅读:
    DOS 错误代码
    WINRAR 建立批处理备份文件
    clear.bat
    continue break
    播放dll中的wav声音
    BAT删除自身del 0
    bat 延时10秒自动关闭自己bat
    产生随机数
    RemoveDirZ.bat
    DELPHI中取整数的方法
  • 原文地址:https://www.cnblogs.com/shamgod/p/5188185.html
Copyright © 2020-2023  润新知