安卓可以显示进度的下载器

学习了安卓多线程之后极客班老师要求做一个下载器,当然老师本身已经提供了非常详细的代码,我就只把代码详细分析,稍微修改下。
下载器页面由EditText、按钮和进度条组成,当点击按钮时根据EditText里的网址来进行下载。在获取下载资源时会得到资源的总大小,用当前已经下载的资源的大小与总大小相处即可得到下载进度。
下面是代码

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static final String GEEK_BAND = "GeekBand";
    private TextView mTextView;
    private Button mDownloadButton;
    private ProgressBar mProgressBar;
    private EditText mEditText;

    public static final String TAG = MainActivity.class.getSimpleName();

    private Handler mHandler = new DownloadHandler(this);

    public TextView getmTextView(){
        return mTextView;
    }

    public ProgressBar getmProgressBar(){
        return mProgressBar;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //UI线程

        setContentView(R.layout.activity_main);
        mTextView = (TextView) findViewById(R.id.text_view);
        mDownloadButton = (Button) findViewById(R.id.download_button);
        mProgressBar = (ProgressBar) findViewById(R.id.progress_bar);
        mEditText = (EditText) findViewById(R.id.url_edit);
        mDownloadButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.download_button:
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //获取输入的网址
                        String apkurl = mEditText.getText().toString();
                        download(apkurl);
                    }
                }).start();
                break;
        }
    }

    private void download(String apkUrl){
        try {
            //与输入的网址链接
            URL url = new URL(apkUrl);
            URLConnection urlConnection = url.openConnection();
            //获取输入流
            InputStream inputStream = urlConnection.getInputStream();
            //要下载的文件大小
            int contentLength = urlConnection.getContentLength();
            //文件名称
            String downloadFoldersName = Environment.getExternalStorageDirectory()+ File.separator +GEEK_BAND +File.separator;
            File file = new File(downloadFoldersName);
            if (!file.exists()){
                file.mkdir();
            }

            String fileName = downloadFoldersName +"test.apk";
            File apkFile = new File(fileName);
            if (apkFile.exists()){
                apkFile.delete();
            }

            int downloadSize = 0;
            byte[] bytes = new byte[1024];
            int length = 0;

            OutputStream outputStream = new FileOutputStream(fileName);
            //用已下载的资源大小除以总资源
            while ((length = inputStream.read(bytes))!= -1){
                outputStream.write(bytes,0,length);
                downloadSize += length;
                int progress = downloadSize*100/contentLength;

                //update UI

                Message message = mHandler.obtainMessage();
                message.obj = progress;
                message.what = 0;
                mHandler.sendMessage(message);

                inputStream.close();
                outputStream.close();
            }

        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static class DownloadHandler extends Handler{
        public final WeakReference<MainActivity> mActivity;

        public DownloadHandler(MainActivity activity) {
            mActivity = new WeakReference<>(activity);
        }

        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            MainActivity activity = mActivity.get();

            switch (msg.what){
                case 0:
                    int progress = (int) msg.obj;
                    activity.getmProgressBar().setProgress(progress);
                    activity.getmTextView().setText("progress"+progress);
                    if (progress == 100){
                        Toast.makeText(activity,"download success",Toast.LENGTH_SHORT).show();
                    }
                    break;
            }
        }
    }
}
当然需要INTERNET和外部读写权限,要记得在安卓的MAinfest文件里加上。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值