使用Xutils框架稳定下载大文件(以下一首歌为例,可以自定义进度条)

<span style="font-size:18px;">1.首先import Module(不会导入的可以看我之前写的博客哈,好像叫什么来着,正确导入第三方框架什么的.....)</span>
<span style="font-size:18px;">2.写个UI(感觉自己写的UI不是特别完美...)</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="mrzhou.com.xutils_demo.MainActivity"
    tools:showIn="@layout/activity_main">


    <Button
        android:id="@+id/btn_download"
        android:text="@string/aaa"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="158dp"/>


    <Button
        android:id="@+id/btn_play"
        android:text="@string/ccc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_alignLeft="@+id/btn_download"
        android:layout_alignStart="@+id/btn_download"/>

    <ProgressBar
        android:visibility="invisible"
        android:max="100"
        android:progressDrawable="@drawable/progress_bar"
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:id="@+id/pb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/btn_download"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>

    <TextView
        android:visibility="invisible"
        android:id="@+id/pb_tv"
        android:text="@string/xxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/btn_play"
        android:layout_centerHorizontal="true"/>


</RelativeLayout></span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
</span>
<span style="font-size:18px;">
<span style="font-family: Arial, Helvetica, sans-serif;">然后在strings.xml中</span></span>
</span>
<span style="font-size:18px;"><pre name="code" class="html"><span style="font-size:18px;"><resources>
    <string name="app_name">xutils_demo</string>
    <string name="action_settings">Settings</string>
    <string name="aaa">下载</string>
    <string name="ccc">开始播放</string>
    <string name="xxx">已下载: 0%</string>
</resources>

</span>




在drawable目录写个自定义样式的进度条  progress_bar.xml  ,感觉自己还是挺追求完美的,hhh...

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />

        <solid android:color="#88000000"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="270"
                    android:centerColor="#C6B7FF"
                    android:centerY="0.75"
                    android:endColor="#C3B2FF"
                    android:startColor="#B9A4FF" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

            <gradient
                android:angle="270"
                android:centerColor="#74EBFF"
                android:centerY="0.75"
                android:endColor="#8EEFFF"
                android:startColor="#57E8FF" />
            </shape>
        </clip>
    </item>

</layer-list></span></span></span>



3.然后写主代码了


<span style="font-size:18px;">import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
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;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    private Button mBtn_play;
    private Button mbtn_download;

    private final String downloadUrl = "http://so1.111ttt.com:8282/2016/1/02/25/195251206359.mp3?tflag=1463900812&pin=42ee73a813fa9b1737c70bec3a91d1fe";
    private final String target = Environment.getExternalStorageDirectory().getAbsolutePath()+"/aaa.mp3";
    private ProgressBar mPb;
    private boolean download_status = false;//判断是否下载
    private TextView mPb_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();


        // 显示两种进度条
        setProgressBarVisibility(true);


        new Thread() {
            @Override
            public void run() {
                initEvent();
            }
        }.start();

    }

    private void initEvent() {
        mbtn_download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //1.判断sd卡是否挂载
                if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                    HttpUtils http = new HttpUtils();

                    Log.i("tag", target);

                    http.download(downloadUrl, target, new RequestCallBack<File>() {
                        @Override
                        public void onSuccess(ResponseInfo<File> responseInfo) {
                            Log.i("tag", "下载成功");

                            //下载成功后,隐藏进度条
                            mPb.setVisibility(View.INVISIBLE);
                            mPb_tv.setVisibility(View.INVISIBLE);
                            mBtn_play.setEnabled(true);

                            download_status = true;

                            // File file = responseInfo.result;
                            Toast.makeText(getApplicationContext(), "音乐文件已下载完成", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(HttpException error, String msg) {

                            Log.i("tag", "下载失败....");


                        }

                        @Override
                        public void onStart() {
                            super.onStart();
                            Log.i("tag", "开始下载....");

                            mBtn_play.setEnabled(false);
                            mbtn_download.setEnabled(false);

                            Toast.makeText(getApplicationContext(),"正在下载中,请稍后...",Toast.LENGTH_SHORT).show();

                            mPb.setVisibility(View.VISIBLE);
                            mPb_tv.setVisibility(View.VISIBLE);

                        }

                        @Override
                        public void onLoading(long total, long current, boolean isUploading) {
                            super.onLoading(total, current, isUploading);
/*
                            Log.i("tag", "tatal:  " + total);
                            Log.i("tag", "current:  " + current);
                            Log.i("tag", "isUploading:  " + isUploading);
*/

                            //百分比
                            int percent = (int) (((float)current / total) * 100);

                            mPb.setProgress(percent);
                            mPb_tv.setText("已下载: "+percent+" %");

                        }
                    });


                } else {
                    Toast.makeText(getApplicationContext(), "你的SD卡未挂载....", Toast.LENGTH_SHORT).show();
                }
            }
        });


        mBtn_play.setOnClickListener(new View.OnClickListener() {

            private MediaPlayer mMp;

            @Override
            public void onClick(View v) {

                if(download_status == false)
                {
                    Toast.makeText(getApplicationContext(),"歌曲未下载,请先下载...亲",Toast.LENGTH_SHORT).show();
                    return;
                }
                if(mMp == null)
                mMp = new MediaPlayer();
                try {
                    mMp.setDataSource(target);
                    
                    //准备播放设置,不写会报错..
                    mMp.prepare();
                    mMp.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

    }

    private void initView() {
        mBtn_play = (Button) findViewById(R.id.btn_play);
        mbtn_download = (Button) findViewById(R.id.btn_download);
        mPb = (ProgressBar) findViewById(R.id.pb);
        mPb_tv = (TextView) findViewById(R.id.pb_tv);
    }

}</span>


最后注意加一下权限

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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值