• 使用xUtils框架实现下载断点续传


    package com.example.kuangjiadownload;
    
    import java.io.File;
    
    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 android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.ProgressBar;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        private String path = "http://192.168.21.1:8080/ok/TGPSetup.exe";
        private TextView tv;
        private ProgressBar pro;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            tv = (TextView) findViewById(R.id.tv);
            pro = (ProgressBar) findViewById(R.id.pro);
            findViewById(R.id.btn).setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    HttpUtils httpUtils = new HttpUtils();
                    //要下载的地址,文件保存路径,支持断点续传,支持重命名,回调方法
                    httpUtils.download(path, "sdcard/TGPSetup.exe", true, true,
                            new RequestCallBack<File>() {
                                
                                @Override
                                public void onSuccess(ResponseInfo<File> arg0) {
                                    Toast.makeText(MainActivity.this,"ok", 0).show();
                                    
                                }
                                
                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    Toast.makeText(MainActivity.this,"ok", 0).show();
                                    
                                }
                                @Override
                                public void onLoading(long total, long current,
                                        boolean isUploading) {
                                    // TODO Auto-generated method stub
                                    super.onLoading(total, current, isUploading);
                                    pro.setMax((int)total);
                                    pro.setProgress((int)current);
                                    tv.setText(current*100/total+"%");
                                    
                                }
                            });
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }
    
    }
    <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: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=".MainActivity"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="点击下载" />
        <ProgressBar 
            android:id="@+id/pro"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            style="@android:style/Widget.ProgressBar.Horizontal"
            
            />
        <TextView 
            android:id="@+id/tv"
              android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
    
    </LinearLayout>
  • 相关阅读:
    Java StringTokenizer Example
    java 删除字符串中的特定字符
    [Python]网络爬虫(二):利用urllib2通过指定的URL抓取网页内容
    Uniform resource name
    [Python]网络爬虫(一):抓取网页的含义和URL基本构成
    coco2dx 精灵类
    window和nodejs作用域区别(待续)
    ubuntu开机遇到-您的当前网络有.local域,我们不建议这样做而且这与AVAHI网络服务探测不兼容。该服务已被禁用
    ruby中的reject和reject!
    ruby中将数组转换成hash
  • 原文地址:https://www.cnblogs.com/84126858jmz/p/4966841.html
Copyright © 2020-2023  润新知