zoukankan      html  css  js  c++  java
  • 使用第三方框架xUtils2进行下载

    AndroidManifest.xml中添加权限

     <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.msi.xutils.MainActivity">
    
    
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="下载"
            android:onClick="click"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_failure"/>
    
        <ProgressBar
            android:id="@+id/pb_download"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Horizontal" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/tv_progressBar"/>
    </LinearLayout>
    

      

    MainActivity代码

    package com.example.msi.xutils;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.lidroid.xutils.HttpUtils;
    import com.lidroid.xutils.exception.HttpException;
    import com.lidroid.xutils.http.ResponseInfo;
    import com.lidroid.xutils.http.callback.RequestCallBack;
    
    import java.io.File;
    
    public class MainActivity extends AppCompatActivity {
    
    
        private TextView tv_failure;
        private TextView tv_pargressBar;
        private ProgressBar pb_download;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv_failure = (TextView) findViewById(R.id.tv_failure);
            tv_pargressBar = (TextView) findViewById(R.id.tv_progressBar);
            pb_download = (ProgressBar) findViewById(R.id.pb_download);
        }
    
    
        public void click(View v){
            HttpUtils http = new HttpUtils();
            String fileName = "TeamViewer.exe";
            String webPath = "http://172.17.11.161:8080/" + fileName;
    
    
            http.download(webPath,//下载地址
                    "sdcard/TeamViewer.exe",//文件保存路径
                    true,//断点续传
                    true, //自动重命名
                    new RequestCallBack<File>() {
    
                        @Override
                        public void onSuccess(ResponseInfo<File> responseInfo) {
                            Toast.makeText(MainActivity.this,responseInfo.result.getPath(),Toast.LENGTH_SHORT).show();
                        }
    
                        @Override
                        public void onLoading(long total, long current, boolean isUploading) {
                            super.onLoading(total, current, isUploading);
                            tv_pargressBar.setText(current * 100 /total + "%");
                            pb_download.setMax((int)total);
                            pb_download.setProgress((int)current);
                        }
    
                        @Override
                        public void onFailure(HttpException e, String s) {
                            tv_failure.setText(s);
                        }
    
            });
        }
    }
    

      

  • 相关阅读:
    HTML表格和列表笔记&练习<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>关于表格的一些练习</title> </head> <body> <p>一个普通基本的表格</p> <table border="5&
    HTML中的CSS类型
    html关于图片和链接的笔记
    实现窗体背景透明
    HTML控件篇 -- input
    AngularJs的$http使用随笔
    Win7启动修复(Ubuntu删除后进入grub rescue的情况)
    2013年9月30日我的博客园开通啦
    将excel表格导入到DataGridView
    c#中,点击一个菜单项后调用exe文件
  • 原文地址:https://www.cnblogs.com/mlsq2015/p/5576164.html
Copyright © 2011-2022 走看看