[size=1em]1
[size=1em]2
[size=1em]3
[size=1em]4
[size=1em]5
[size=1em]6
[size=1em]7
[size=1em]8
[size=1em]9
[size=1em]10
[size=1em]11
[size=1em]12
[size=1em]13
[size=1em]14
[size=1em]15
[size=1em]16
[size=1em]17
[size=1em]18
[size=1em]19
[size=1em]20
[size=1em]21
[size=1em]22
[size=1em]23
[size=1em]24
[size=1em]25
[size=1em]26
[size=1em]27
[size=1em]28
[size=1em]29
[size=1em]30
[size=1em]31
[size=1em]32
[size=1em]33
[size=1em]34
[size=1em]35
[size=1em]36
[size=1em]37
[size=1em]38
[size=1em]39
[size=1em]40
[size=1em]41
[size=1em]42
[size=1em]43
[size=1em]44
[size=1em]45
[size=1em]46
[size=1em]47
| [size=1em][size=1em]public class HandlerTestActivity extends Activity {
[size=1em] private TextView tv;
[size=1em] private static final int UPDATE = 0;
[size=1em] private Handler handler = new Handler() {
[size=1em] @Override
[size=1em] public void handleMessage(Message msg) {
[size=1em] // TODO 接收消息并且去更新UI线程上的控件内容
[size=1em] if (msg.what == UPDATE) {
[size=1em] // Bundle b = msg.getData();
[size=1em] // tv.setText(b.getString("num"));
[size=1em] tv.setText(String.valueOf(msg.obj));
[size=1em] }
[size=1em] super.handleMessage(msg);
[size=1em] }
[size=1em] };
[size=1em] /** Called when the activity is first created. */
[size=1em] @Override
[size=1em] public void onCreate(Bundle savedInstanceState) {
[size=1em] super.onCreate(savedInstanceState);
[size=1em] setContentView(R.layout.main);
[size=1em] tv = (TextView) findViewById(R.id.tv);
[size=1em] new Thread() {
[size=1em] @Override
[size=1em] public void run() {
[size=1em] // TODO 子线程中通过handler发送消息给handler接收,由handler去更新TextView的值
[size=1em] try {
[size=1em] for (int i = 0; i < 100; i++) {
[size=1em] Thread.sleep(500);
[size=1em] Message msg = new Message();
[size=1em] msg.what = UPDATE;
[size=1em] // Bundle b = new Bundle();
[size=1em] // b.putString("num", "更新后的值:" + i);
[size=1em] // msg.setData(b);
[size=1em] msg.obj = "更新后的值:" + i;
[size=1em] handler.sendMessage(msg);
[size=1em] }
[size=1em] } catch (InterruptedException e) {
[size=1em] e.printStackTrace();
[size=1em] }
[size=1em] }
[size=1em] }.start();
[size=1em] }
[size=1em]}
|