• Androidの多线程之更新ui(Thread+Handler+Message)


    流程:

    1.点击开始,显示进度对话框,启动线程,循环发送消息(1-100)

    2.更新对话框中的进度,和标题栏的进度

    3.到达最大进度值时,关闭对话框,标题栏显示更新完成

    解析:

    1.在主线程处理handler的消息,更新Ui

    2.在其他线程里用主线程的handler不停发送消息

    3.线程启动,发送消息

     1 package com.bvin.thread;
     2 
     3 import android.app.Activity;
     4 import android.app.ProgressDialog;
     5 import android.os.Bundle;
     6 import android.os.Handler;
     7 import android.os.Message;
     8 import android.util.Log;
     9 import android.view.View;
    10 import android.view.Window;
    11 import android.widget.Button;
    12 import android.widget.TextView;
    13 
    14 public class MainrActivity extends Activity {
    15     /** Called when the activity is first created. */
    16     private Handler handler;
    17     private ProgressDialog dialog;
    18     
    19     private TextView tv;
    20     
    21     @Override
    22     public void onCreate(Bundle savedInstanceState) {
    23         super.onCreate(savedInstanceState);
    24         this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
    25         setContentView(R.layout.main);
    26         setSecondaryProgress(10000); //设置副进度为10000
    27         handler = new Handler(){
    28 
    29             @Override
    30             public void handleMessage(Message msg) {
    31                 // TODO Auto-generated method stub
    32                 super.handleMessage(msg);
    33                 Log.e("progress:", ""+msg.what);
    34                 setTitle("更新--"+msg.what);
    35                 setProgress(msg.what*100);
    36                 if(msg.what==100){
    37                     setProgressBarVisibility(false); // 执行完后使进度条隐藏
    38                     dialog.dismiss();
    39                     setTitle("更新完成");
    40                 }
    41             }
    42             
    43             
    44         };
    45         initViews();
    46         
    47     }
    48     
    49     private void initViews(){
    50         
    51         dialog= new ProgressDialog(MainrActivity.this);
    52         dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    53         dialog.setMax(100);
    54         Button bt = (Button)findViewById(R.id.bt);
    55         bt.setOnClickListener(new View.OnClickListener() {
    56             
    57             @Override
    58             public void onClick(View v) {
    59                 // TODO Auto-generated method stub
    60                 setProgressBarVisibility(true);
    61                 dialog.show();
    62                 exec();
    63             }
    64         });
    65         
    66         tv  = (TextView)findViewById(R.id.tv);
    67         
    68         
    69         
    70     }
    71     
    72     private void exec(){
    73         
    74         new Thread(new Runnable(){
    75 
    76             @Override
    77             public void run() {
    78                 // TODO Auto-generated method stub
    79                 for(int i=0;i<=100;i++){
    80                     
    81                     handler.sendEmptyMessage(i);
    82                 }
    83             }
    84             
    85             
    86         }).start();
    87     }
    88 }
  • 相关阅读:
    Codeforces Round #592 (Div. 2)C. The Football Season(暴力,循环节)
    Educational Codeforces Round 72 (Rated for Div. 2)D. Coloring Edges(想法)
    扩展KMP
    poj 1699 Best Sequence(dfs)
    KMP(思路分析)
    poj 1950 Dessert(dfs)
    poj 3278 Catch That Cow(BFS)
    素数环(回溯)
    sort与qsort
    poj 1952 buy low buy lower(DP)
  • 原文地址:https://www.cnblogs.com/bvin/p/2782803.html
Copyright © 2020-2023  润新知