作者在 2016-01-03 14:52:05 发布以下内容
public class MainActivity extends Activity {
private Button btn;
private ProgressDialog dialog; //进度条
private MyAsyncTasck myAsyncTasck;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button)this.findViewById(R.id.button1);
btn.setOnClickListener(new ButtonClickListener());
}
//点击按钮的监听
class ButtonClickListener implements OnClickListener{
@Override
public void onClick(View view) {
myAsyncTasck = new MyAsyncTasck();
myAsyncTasck.execute();
}
}
class MyAsyncTasck extends AsyncTask<String,Integer,Object>{
//准备前
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = (ProgressDialog)new ProgressDialog(MainActivity.this);
dialog.setTitle("下载中....");
dialog.setMessage("mp3文件下载中");
dialog.setMax(100); //最大长度
//风格样式
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
//当前位置
dialog.setProgress(0);
dialog.setIndeterminate(false);
//处理Bug
dialog.show();
}
//前台线程
@Override
protected void onProgressUpdate(Integer... values) {
super.onProgressUpdate(values);
dialog.setProgress(values[0]);
}
//后台线程
@Override
protected Object doInBackground(String... arg0) {
try {
URL url = new URL("http://10.0.2.2:8080/TestHandle/ylxb.mp3");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//获取文件总长度
int totalLength = connection.getContentLength();
File file = new File("/mnt/sdcard/1234.mp3");
file.createNewFile();//创建文件
int baifen = 0;
//分长度
int fen = totalLength/5;
int stop=0;
for (int i = 0; i < 5; i++) {
stop=(i+1)*fen;
MyThread myThread = new MyThread(url, file,i*fen+1, stop,fen);
if (i==4) {
stop=totalLength;
}
myThread.start();
//baifen = (int)(fen/totalLength)*100;
//传参数给前台修改进度条
//onProgressUpdate(baifen);
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//dialog.dismiss(); //进度条消失
}
}
class MyThread extends Thread{
private URL url;
private File file;
private int StartPostion;
private int StopPostion;
private int len;
private ProgressDialog dialog;
public MyThread(URL url, File file, int startPostion, int stopPostion,
int len) {
super();
this.url = url;
this.file = file;
StartPostion = startPostion;
StopPostion = stopPostion;
this.len = len;
}
@Override
public void run() {
super.run();
HttpURLConnection connection;
try {
connection = (HttpURLConnection) url.openConnection();
System.out.println(StartPostion+"----"+StopPostion);
connection.setRequestProperty("Range", "bytes="+StartPostion+"-"+StopPostion);
connection.setAllowUserInteraction(true); //允许用户采用断电分割
InputStream inputStream = connection.getInputStream();
OutputStream outputStream = new FileOutputStream(file);
//转换成底层流
BufferedInputStream inputStream1 = new BufferedInputStream(inputStream);
RandomAccessFile outputSteam1 = new RandomAccessFile(file, "rw");
outputSteam1.seek((long) StartPostion); //一开始的位置
byte[] bb = new byte[1024];
int data=0;
int baifen=0;
while((data=inputStream1.read(bb))!=-1){
//System.out.println(data);
outputSteam1.write(bb);
Message message=new Message();
Bundle bun=new Bundle();
baifen = (int)(len/data)*100;
bun.putInt("jd", baifen);
message.setData(bun);
send.sendMessage(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
SendView send=new SendView();
class SendView extends Handler{
public void handleMessage(Message msg) {
super.handleMessage(msg);
Bundle bun=msg.getData();
int len=bun.getInt("jd");
dialog.setProgress(len);
}
}
}
-------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jinduntiao"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<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="com.example.jinduntiao.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>