安卓Xutils框架---断点续传

这篇博客深入探讨了如何利用Xutils框架在安卓应用中实现断点续传的功能,提供了详细的操作步骤和代码示例。
摘要由CSDN通过智能技术生成

上一篇浅谈了Xutlis框架的基本信息

本篇主要讲一下 如何 用 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>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值