实现一个mp3文件下载功能,并展示进度
1.
首先是权限的声明
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.fanhy.f9_download" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="17" /> 10 11 <application 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="com.fanhy.f9_download.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 </application> 26 <uses-permission android:name="android.permission.INTERNET"/> 27 <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 28 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 29 </manifest>
1 package com.fanhy.f9_download; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.app.ProgressDialog; 6 import android.content.DialogInterface; 7 import android.util.Log; 8 import android.view.Menu; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.widget.Button; 12 /** 13 * 实现一个mp3文件下载功能,并展示进度 14 * */ 15 public class MainActivity extends Activity { 16 ProgressDialog pd; 17 ProgressHandler handler; 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 23 Button btn = (Button) findViewById(R.id.btn); 24 25 26 btn.setOnClickListener(new OnClickListener() { 27 28 @Override 29 public void onClick(View v) { 30 // 创建进度对话框对象 31 createDialog(); 32 // 创建ProgressHandler对象 33 handler = new ProgressHandler(pd,MainActivity.this); 34 // 新起线程进行网络任务 35 new Thread(new Runnable() { 36 37 @Override 38 public void run() { 39 int result = new DownLoader(MainActivity.this,handler).downloadFile("http://yinyueshiting.baidu.com/data2/music/134369899/2927661428987661128.mp3?xcode=e5bea9662f165be741e9faede5f96daae6bd1fd285a802a4", "", "down.mp3"); 40 Log.d("fanhy", "下载结果码:"+result); 41 } 42 }).start(); 43 } 44 }); 45 46 } 47 48 private void createDialog() { 49 pd = new ProgressDialog(this); 50 pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 51 pd.setMax(100); 52 pd.setTitle("正在下载"); 53 } 54 55 56 }
2.
1 package com.fanhy.f9_download; 2 3 import java.io.FileOutputStream; 4 import java.io.InputStream; 5 import java.net.HttpURLConnection; 6 import java.net.URL; 7 8 import android.content.Context; 9 import android.os.Message; 10 import android.util.Log; 11 12 public class DownLoader { 13 private URL url = null; 14 Context context; 15 ProgressHandler handler; 16 17 public DownLoader(Context context, ProgressHandler handler) { 18 super(); 19 this.context = context; 20 this.handler = handler; 21 } 22 23 // 从输入流读取数据,用输出流写入手机存储 24 public int downloadFile(String urlStr, String path, String name) { 25 InputStream input = null; 26 try { 27 url = new URL(urlStr); 28 // 打开链接 29 HttpURLConnection httpc = (HttpURLConnection) url.openConnection(); 30 // 以GET方式请求数据 31 httpc.setRequestMethod("GET"); 32 // 设置网络超时连接时间为5秒 33 httpc.setConnectTimeout(5 * 1000); 34 // 连接 35 httpc.connect(); 36 if(httpc.getResponseCode() == HttpURLConnection.HTTP_OK){ 37 // 得到网络输入流 38 input = httpc.getInputStream(); 39 40 // 下载前获取文件大小 41 int fileSize = httpc.getContentLength(); 42 Log.d("fanhy", "文件大小为:"+fileSize); 43 44 // 得到文件输出流 45 FileOutputStream fos = context.openFileOutput(path + name, 46 Context.MODE_PRIVATE); 47 int len = 0; 48 byte[] buf = new byte[128]; 49 50 // 假设最大进度为100,每次读写128字节,已得到文件大小,计算循环几次进度加1 51 int count = fileSize/128/100; 52 int progress = 0; 53 int num = 0; 54 // 循环读写 55 while ((len = input.read(buf)) > 0) { 56 57 num++; 58 // 每循环count次,进度加1,并发出消息让handler更新进度 59 if(num == count){ 60 progress++; 61 Message msg = Message.obtain(); 62 msg.arg1 = progress; 63 handler.sendMessage(msg); 64 num = 0; 65 } 66 fos.write(buf, 0, len); 67 } 68 } else{ 69 // 返回码不为HTTP_OK则抛出异常,下载失败 70 new RuntimeException("下载失败!"); 71 } 72 } catch (Exception e) { 73 //e.printStackTrace(); 74 return -1; // 数据异常则返回-1 75 } 76 return 0; // 数据正常的时候返回0 77 } 78 }
3.
1 package com.fanhy.f9_download; 2 3 import android.app.ProgressDialog; 4 import android.content.Context; 5 import android.os.Handler; 6 import android.os.Message; 7 import android.widget.Toast; 8 9 public class ProgressHandler extends Handler { 10 ProgressDialog dialog; 11 Context context; 12 13 public ProgressHandler(ProgressDialog dialog, Context context) { 14 super(); 15 this.dialog = dialog; 16 this.context = context; 17 } 18 19 @Override 20 public void handleMessage(Message msg) { 21 // TODO Auto-generated method stub 22 super.handleMessage(msg); 23 24 int progress = msg.arg1; 25 // 进度为1时展示对话框 26 if (progress == 1) { 27 dialog.show(); 28 } 29 // 更新进度条对话框 30 dialog.setProgress(progress); 31 32 // 进度为100时弹提示,并取消对话框 33 if (progress == 100) { 34 dialog.dismiss(); 35 Toast.makeText(context, "文件已下载完毕", Toast.LENGTH_SHORT).show(); 36 } 37 } 38 }
浙公网安备 33010602011771号