zoukankan      html  css  js  c++  java
  • Android更新下载进度条

    下载文件会阻塞UI主线程,所以需要new一个新线程来执行下载操作,通过handler执行更新UI进度条操作。代码如下:

    public class AndroidTest extends Activity {
        private static final String TAG = "AndroidTest";
    
        private ProgressBar progressBar = null;
        private Button startButton = null;
        private EditText filenameText = null;
        private MyHandler handler = null;
    
        private Message message = null;
        private boolean flag = true;
        private int size = 1;
        private int hasRead = 0;
        private int len = 0;
        private byte buffer[] = new byte[1024*4];
        private int index = 0; 
        
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            
            progressBar = (ProgressBar)findViewById(R.id.progress_horizontal);
            startButton = (Button)findViewById(R.id.mybutton);
            startButton.setOnClickListener(new ButtonClick());
        
            filenameText = (EditText)findViewById(R.id.fileNameID);
        
            handler = new MyHandler();
        }
    
    
        public boolean downloadFile(final String urlStr, final String filename) {
            new Thread(new Runnable(){  
                public void run() { 
                    try {
                        URL url = new URL(urlStr);
                        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
                        size = connection.getContentLength();
                        InputStream inputStream = connection.getInputStream();
                        OutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory()+"/"+filename);
                        
                        while((len=inputStream.read(buffer))!=-1){
                            outputStream.write(buffer);
                            hasRead+=len;
                            index = (int)(hasRead*100)/size;
                            message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                            Log.d(TAG, "index = " + index);
                            System.out.println("has = "+hasRead+" size = "+size+" index = "+index);
                        }
                    
                        inputStream.close();
                        outputStream.close();
                    } catch (Exception e) {
                        flag = false;
                        e.printStackTrace();
                    }
                }
            }).start();
            
            return flag;
        }
    
        class ButtonClick implements OnClickListener {
    
            public void onClick(View v) {
        
                String url = filenameText.getText().toString();
                String filename = url.substring(url.lastIndexOf('/') + 1);
                Log.d(TAG, "url = " + url);
                Log.d(TAG, "filename = " + filename);
                
                if(!downloadFile(url, filename)) {
                    String rs = "下载失败 ";
                    Toast.makeText(AndroidTest.this, rs, Toast.LENGTH_SHORT).show();
                }
        
            }
    
        }
    
        class MyHandler extends Handler{
    
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    progressBar.setProgress(index);
                    Log.d(TAG, "setProgress index:" + index);
                    if (index >= 99) {
                        String rs = "下载完成";
                        Toast.makeText(AndroidTest.this, rs, Toast.LENGTH_SHORT).show();
                    }
                }
                
                super.handleMessage(msg);
            }
    
        }
    
    }

    转:http://blog.csdn.net/ameyume/article/details/6183462

  • 相关阅读:
    Array.prototype.slice.call()
    闭包与变量
    XML处理指令
    XSLT学习(九)通过JavaScript转化xml
    chrome浏览器canvas画图不显示
    B.储物点的距离
    A.约数个数的和
    F.求最大值
    STVD+COSMIC编译工程时出现Error creating process for executable mapinfo
    STVD+COSMIC编译工程时can't open file crtsi0.sm8
  • 原文地址:https://www.cnblogs.com/gzggyy/p/3085553.html
Copyright © 2011-2022 走看看