日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android使用开源项目Xutils实现多线程下载文件

發(fā)布時間:2025/3/19 Android 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android使用开源项目Xutils实现多线程下载文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#1.下載utils項目

https://github.com/wyouflf/xUtils


#2布局文件里實現(xiàn)UI


<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"tools:context=".MainActivity" ><EditTextandroid:id="@+id/tv_path"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="http://down.360safe.com/inst.exe" /><Buttonandroid:layout_marginTop="10dip"android:layout_width="match_parent"android:onClick="download"android:layout_height="wrap_content"android:text="下載"/><TextViewandroid:id="@+id/tv_info"android:layout_width="match_parent"android:layout_marginTop="10dip"android:layout_height="wrap_content"android:text="提示信息"/></LinearLayout>

#3.在mainactivity中實現(xiàn)代碼功能

package com.wzw.downloaddemo;import java.io.File;import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.HttpHandler; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack;import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.TextView;public class MainActivity extends Activity {private EditText etPath;private TextView tvInfo;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);etPath=(EditText) findViewById(R.id.tv_path);tvInfo=(TextView) findViewById(R.id.tv_info);}public void download(View v){String path =etPath.getText().toString();HttpUtils http = new HttpUtils();http.download(path, "/sdcard/360.exe", true, true, new RequestCallBack<File>() {<span style="white-space:pre"> </span>@Overridepublic void onStart() {tvInfo.setText("正在連接...");}@Overridepublic void onLoading(long total, long current, boolean isUploading) {tvInfo.setText(current + "/" + total);}@Overridepublic void onFailure(HttpException error, String msg) {tvInfo.setText(msg);}@Overridepublic void onSuccess(ResponseInfo<File> responseInfo) {// TODO Auto-generated method stubtvInfo.setText("downloaded:" + responseInfo.result.getPath());}});}}

用Utils的話比用傳統(tǒng)的方式要方便的多。


總結(jié)

以上是生活随笔為你收集整理的Android使用开源项目Xutils实现多线程下载文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。