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

一、代码
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?

				
时间: 2024-12-28 01:49:26

ANDROID_MARS学习笔记_S01原始版_008_Handler(异步消息处理机制)的相关文章

ANDROID_MARS学习笔记_S01原始版_013_广播机制二

一.代码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_pa

ANDROID_MARS学习笔记_S01原始版_012_广播机制一

一.简介 二.代码1.xml(1)activity_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

ANDROID_MARS学习笔记_S01原始版_007_Handler及线程的简单使用

一.运行结果 一.代码1.xml(1)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_heig

ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过android.media.MediaPlayer实现播放,暂停.停止 二.代码1.xml(1)player.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:andr

ANDROID_MARS学习笔记_S01原始版_010_ContentProvider

一.简介 一.代码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" android:layout_width="fill

ANDROID_MARS学习笔记_S01原始版_005_ProgressBar

一.代码 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_p

ANDROID_MARS学习笔记_S01原始版_006_ListView

一.代码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" android:layout_width="fill_pare

ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

一.代码 1.xml(1)radio.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_

ANDROID_MARS学习笔记_S01原始版_004_TableLayout

1.xml 1 <?xml version="1.0" encoding="utf-8"?> 2 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="fill_parent" 4 android:layout_height="fill_parent"