关闭

android_37_使用开源xUtils进行下载

标签: androidxUtils下载
99人阅读 评论(0) 收藏 举报
分类:


将Jar包copy到libs目录下


效果:








清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sg31.xutils"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    // 注意权限 
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




布局:

<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="com.sg31.xutils.MainActivity" 
    android:orientation="vertical"
    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="下载" 
        android:onClick="downloadBtnClicked"
        />
    <TextView
        android:id="@+id/tv_failure"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />
    <ProgressBar
        android:id="@+id/pb" 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@android:style/Widget.ProgressBar.Horizontal"
        />
    <TextView
        android:id="@+id/tv_progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         />

</LinearLayout>




代码:

package com.sg31.xutils;

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.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

	private TextView tv_failure;
    private TextView tv_progress;
    private ProgressBar pb;
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        tv_failure = (TextView) findViewById(R.id.tv_failure);
        tv_progress = (TextView) findViewById(R.id.tv_progress);
        pb = (ProgressBar) findViewById(R.id.pb);
        
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    
    public void downloadBtnClicked(View v){
        HttpUtils utils = new HttpUtils();
        
        String fileName = "NEXT_TO_YOU.mp3";  
        // 确定下载地址  
        String path = "http://od8vj43og.bkt.clouddn.com/" + fileName;  
        utils.download(path, //下载地址
                       "sdcard/"+fileName, //文件保存路径
                       true,//是否支持断点续传
                       true, new RequestCallBack<File>() {
                           
                           //下载成功后调用
                           @Override
                           public void onSuccess(ResponseInfo<File> arg0) {
                               Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();
                           }
                           
                           //下载失败调用
                           @Override
                           public void onFailure(HttpException arg0, String arg1) {
                               tv_failure.setText(arg1);
                           }
                           
                           @Override
                           public void onLoading(long total, long current,
                                                 boolean isUploading) {
                               super.onLoading(total, current, isUploading);
                               pb.setMax((int)total);
                               pb.setProgress((int)current);
                               tv_progress.setText(current * 100 / total + "%");
                           }
						 
                       });
    }
    
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:360574次
    • 积分:8373
    • 等级:
    • 排名:第2342名
    • 原创:472篇
    • 转载:2篇
    • 译文:0篇
    • 评论:75条
    最新评论