EX04_17.java
上传用户:vip_99
上传日期:2021-03-27
资源大小:61159k
文件大小:4k
源码类别:

android开发

开发平台:

Java

  1. package irdc.ex04_17;
  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.os.Message;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ProgressBar;
  9. import android.widget.TextView;
  10. public class EX04_17 extends Activity
  11. {
  12.   private TextView mTextView01;
  13.   private Button mButton01;
  14.   private ProgressBar mProgressBar01;
  15.   public int intCounter=0;
  16.   
  17.   /* 自定义Handler信息代码,用以作为识别事件处理 */
  18.   protected static final int GUI_STOP_NOTIFIER = 0x108;
  19.   protected static final int GUI_THREADING_NOTIFIER = 0x109;
  20.   
  21.   /** Called when the activity is first created. */
  22.   @Override
  23.   public void onCreate(Bundle savedInstanceState)
  24.   {
  25.     super.onCreate(savedInstanceState);
  26.     setContentView(R.layout.main);
  27.     
  28.     mButton01 = (Button)findViewById(R.id.myButton1); 
  29.     mTextView01 = (TextView)findViewById(R.id.myTextView1);
  30.     
  31.     /* 设置ProgressBar widget对象 */
  32.     mProgressBar01 = (ProgressBar)findViewById(R.id.myProgressBar1);
  33.     
  34.     /* 调用setIndeterminate方法赋值indeterminate模式为false */
  35.     mProgressBar01.setIndeterminate(false);
  36.     
  37.     /* 当点击按钮后,开始运行线程工作 */
  38.     mButton01.setOnClickListener(new Button.OnClickListener()
  39.     {
  40.       @Override
  41.       public void onClick(View v)
  42.       {
  43.         // TODO Auto-generated method stub
  44.         
  45.         /* 点击按钮让ProgressBar显示 */
  46.         mTextView01.setText(R.string.str_progress_start);
  47.         
  48.         /* 将隐藏的ProgressBar显示出来 */
  49.         mProgressBar01.setVisibility(View.VISIBLE);
  50.         
  51.         /* 指定Progress为最多100 */
  52.         mProgressBar01.setMax(100);
  53.         
  54.         /* 初始Progress为0 */
  55.         mProgressBar01.setProgress(0);
  56.         
  57.         /* 起始一个运行线程 */
  58.         new Thread(new Runnable()
  59.         {
  60.           public void run()
  61.           {
  62.             /* 默认0至9,共运行10次的循环叙述 */
  63.             for (int i=0;i<10;i++)
  64.             {
  65.               try
  66.               {
  67.                 /* 成员变量,用以识别加载进度 */
  68.                 intCounter = (i+1)*20;
  69.                 /* 每运行一次循环,即暂停1秒 */
  70.                 Thread.sleep(1000);
  71.                 
  72.                 /* 当Thread运行5秒后显示运行结束 */
  73.                 if(i==4)
  74.                 {
  75.                   /* 以Message对象,传递参数给Handler */
  76.                   Message m = new Message();
  77.                   
  78.                   /* 以what属性指定User自定义 */
  79.                   m.what = EX04_17.GUI_STOP_NOTIFIER;
  80.                   EX04_17.this.myMessageHandler.sendMessage(m);
  81.                   break;
  82.                 }
  83.                 else
  84.                 {
  85.                   Message m = new Message();
  86.                   m.what = EX04_17.GUI_THREADING_NOTIFIER;
  87.                   EX04_17.this.myMessageHandler.sendMessage(m);
  88.                 }
  89.               }
  90.               catch(Exception e)
  91.               {
  92.                 e.printStackTrace();
  93.               }
  94.             }
  95.           }
  96.         }).start();
  97.       }
  98.     });
  99.   }
  100.   
  101.   /* Handler建构之后,会聆听传来的信息代码 */
  102.   Handler myMessageHandler = new Handler()
  103.   {
  104.     // @Override 
  105.     public void handleMessage(Message msg)
  106.     { 
  107.       switch (msg.what)
  108.       { 
  109.         /* 当取得识别为 离开运行线程时所取得的信息 */
  110.         case EX04_17.GUI_STOP_NOTIFIER:
  111.           
  112.           /* 显示运行终了 */
  113.           mTextView01.setText(R.string.str_progress_done);
  114.           
  115.           /* 设置ProgressBar Widget为隐藏 */
  116.           mProgressBar01.setVisibility(View.GONE);
  117.           Thread.currentThread().interrupt();
  118.           break;
  119.           
  120.         /* 当取得识别为 持续在运行线程当中时所取得的信息 */
  121.         case EX04_17.GUI_THREADING_NOTIFIER:
  122.           if(!Thread.currentThread().isInterrupted())
  123.           {
  124.             mProgressBar01.setProgress(intCounter);
  125.             /* 将显示进度显示于TextView当中 */
  126.             mTextView01.setText
  127.             (
  128.               getResources().getText(R.string.str_progress_start)+
  129.               "("+Integer.toString(intCounter)+"%)n"+
  130.               "Progress:"+
  131.               Integer.toString(mProgressBar01.getProgress())+
  132.               "n"+"Indeterminate:"+
  133.               Boolean.toString(mProgressBar01.isIndeterminate())
  134.             );
  135.           }
  136.           break;
  137.       } 
  138.       super.handleMessage(msg); 
  139.     }
  140.   };
  141. }