• 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 }
  • 相关阅读:
    章节十:Selenium
    章节七:csv&excel
    章节十三:协程实践
    章节十二:协程
    章节八:爬取知乎文章
    章节十五:Scrapy实操
    章节十六:复习与反爬虫
    章节十一:定时与邮件
    章节十四:Scrapy框架
    阅读习惯2
  • 原文地址:https://www.cnblogs.com/wizzhangquan/p/2982131.html
Copyright © 2020-2023  润新知