Android&MyThread

MainActivity.java
 1 package com.zachary.activitythread;
 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.KeyEvent;
 8 import android.view.Menu;
 9 import android.widget.TextView;
10 
11 public class MainActivity extends Activity {
12 
13     Handler hd= new Handler(){
14 
15         @Override
16         public void handleMessage(Message msg) {
17             // TODO Auto-generated method stub
18             switch(msg.what){
19                 case 1:
20                     Bundle b = msg.getData();
21                     String str = b.getString("msg");
22                     myTextView.setText(str);
23                     break;
24             }
25         }
26         
27     };
28     private TextView myTextView = null;
29     @Override
30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(R.layout.activity_main);
33         myTextView = (TextView)findViewById(R.id.myTextView);
34         System.out.println("Activity----->"+Thread.currentThread().getId());
35         new MyThread(MainActivity.this).start();
36     }
37 
38     @Override
39     public boolean onCreateOptionsMenu(Menu menu) {
40         // Inflate the menu; this adds items to the action bar if it is present.
41         getMenuInflater().inflate(R.menu.main, menu);
42         return true;
43     }
44 
45     @Override
46     public boolean onKeyDown(int keyCode, KeyEvent event) {
47         // TODO Auto-generated method stub
48         if(keyCode==4)
49             System.exit(0);
50         return super.onKeyDown(keyCode, event);
51     }
52 
53 }
MyThread.java
 1 package com.zachary.activitythread;
 2 
 3 import android.os.Bundle;
 4 import android.os.Message;
 5 
 6 public class MyThread extends Thread{
 7 
 8     int count = 0;
 9     MainActivity activity;
10     boolean flag = true;
11     public MyThread(MainActivity activity){
12         this.activity = activity;
13     }
14     @Override
15     public void run() {
16         // TODO Auto-generated method stub
17         while(flag){
18             if(count>=10)
19                 flag = false;
20             String msg = "第" + ++count +"次更改TextView";
21             Bundle bd = new Bundle();
22             bd.putString("msg", msg);
23             Message tempMessage = new Message();
24             tempMessage.setData(bd);
25             tempMessage.what = 1;
26             activity.hd.sendMessage(tempMessage);
27             System.out.println("MyThread----->" + Thread.currentThread().getId());
28             try {
29                 sleep(1000);
30             } catch (InterruptedException e) {
31                 // TODO Auto-generated catch block
32                 e.printStackTrace();
33             }
34         }
35         super.run();
36     }
37     
38 }

 

posted on 2013-03-26 11:16  Zachary_wiz  阅读(160)  评论(0编辑  收藏  举报

导航