package com.nico;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.configue.Const;
import com.download.DownloadService;
import com.download.DownloadService.MyDownLoadThread;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.ViewDebug.FlagToString;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
public class DownLoadPage extends Activity implements OnClickListener {
Br receiver = null;
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
}
@Override
protected void onStop() {
super.onStop();
}
@Override
protected void onDestroy() {
super.onDestroy();
this.unregisterReceiver(receiver);
Const.isDownloading = false;
}
private ProgressBar pb;
private TextView txt;
private NotificationManager nm;
private Button btn;
public DownloadService ds = new DownloadService();
int percent = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
Intent intent = new Intent(DownLoadPage.this, DownloadService.class);
Bundle bd = new Bundle();
bd.putString("path", Environment.getExternalStorageDirectory()
.getAbsolutePath());
bd.putString("url", Const.url + "test.rar");
intent.putExtras(bd);
startService(intent);
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
setContentView(R.layout.downloadpage);
pb = (ProgressBar) findViewById(R.id.progressbar);
txt = (TextView) findViewById(R.id.loadingtips);
btn = (Button) findViewById(R.id.btn);
btn.setText("停止下载");
btn.setOnClickListener(this);
receiver = new Br();
IntentFilter intentfilter = new IntentFilter();
intentfilter.addAction("com.nico.downloadpage");
this.registerReceiver(receiver, intentfilter);
showNotify();
}
public class Br extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
percent = intent.getIntExtra("percent", 0);
pb.setProgress(percent);
txt.setText(percent + "%");
if (percent == 100) {
txt.setText("下载完成");
btn.setText("下载完成");
nm.cancel(11111);
}
}
}
public void showNotify() {
Notification ntf = new Notification(R.drawable.icon, "下载", System
.currentTimeMillis());
Intent i = new Intent();
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent intent = PendingIntent.getActivity(this, 0, i,
PendingIntent.FLAG_UPDATE_CURRENT);
ntf.setLatestEventInfo(DownLoadPage.this, "下载", "正在下载", intent);
nm.notify(11111, ntf);
}
@Override
public void onClick(View v) {
if (v == btn) {
if (percent < 100 && percent > 0 && Const.isDownloading) {
btn.setText("下载");
stopService(new Intent(DownLoadPage.this, DownloadService.class));
} else if (percent == 100) {
btn.setText("下载完成");
Const.isDownloading = false;
btn.setClickable(false);
} else {
btn.setText("停止下载");
Const.isDownloading = true;
startService(new Intent(DownLoadPage.this,
DownloadService.class));
}
}
}
}
package com.download;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.RandomAccess;
import com.configue.Const;
import com.nico.DownLoadPage;
import com.nico.FileListView;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;
public class DownloadService extends Service {
@Override
public void onDestroy() {
Const.isDownloading = false;
super.onDestroy();
}
private int percent;
String fileUrl;
int fileSize;
boolean isFinish;
String tempFile;
File localFile;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onStart(Intent intent, int startId) {
Bundle bundle = intent.getExtras();
String path1 ="";
String url1 = "";
if (bundle != null) {
path1 = bundle.getString("path");
Const.path = path1;
url1 = bundle.getString("url");
Const.fileurl = url1;
}
fileUrl = Const.fileurl;
tempFile = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
localFile = new File(Const.path + "/" + tempFile);
if (!localFile.exists()) {
if (Const.isDownloading) {
try {
localFile.createNewFile();
Const.downloadsize = 0;
new MyDownLoadThread().start();
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
Const.downloadsize = (int) localFile.length();
int tempSize = getFileRealSize(fileUrl);
if (Const.isDownloading) {
if (Const.downloadsize != tempSize) {
new MyDownLoadThread().start();
}
}
}
}
@Override
public boolean onUnbind(Intent intent) {
return super.onUnbind(intent);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
public long remoteFile() {
return Const.downloadsize;
}
public class MyDownLoadThread extends Thread {
byte[] perSize = new byte[512];
FileOutputStream fos = null;
InputStream is = null;
HttpURLConnection conn = null;
public MyDownLoadThread() {
}
@Override
public void run() {
RandomAccessFile randomAccessFile = null;
try {
URL u = new URL(fileUrl);
try {
conn = (HttpURLConnection) u.openConnection();
conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.1.4322)");
String sProperty = "bytes=" + Const.downloadsize + "-";
conn.setRequestProperty("RANGE", sProperty);
randomAccessFile = new RandomAccessFile(localFile, "rwd");
randomAccessFile.seek(Const.downloadsize);
fileSize = conn.getContentLength();
conn.connect();
is = conn.getInputStream();
int realsize = getFileRealSize(fileUrl);
int readSize = 0;
int length = 0;
int read = 0;
while (Const.isDownloading
&& (read = is.read(perSize, 0, 512)) != -1) {
length = read;
readSize += read;
randomAccessFile.write(perSize, 0, length);
percent = (int) ((localFile.length()+length) * 100 / realsize);
Const.downloadsize += length;
Intent intent = new Intent();
intent.setAction("com.nico.downloadpage");
if (Const.downloadsize == realsize) {
isFinish = true;
Const.isDownloading = false;
percent = 100;
intent.putExtra("percent", percent);
DownloadService.this.sendBroadcast(intent);
break;
}
intent.putExtra("percent", percent);
for (int i = 0; i < 10000; i++) {
if (i == 9999) {
DownloadService.this.sendBroadcast(intent);
}
}
}
} catch (IOException e) {
Const.isDownloading = false;
e.printStackTrace();
}
} catch (MalformedURLException e) {
Const.isDownloading = false;
e.printStackTrace();
} finally {
if (randomAccessFile != null) {
try {
randomAccessFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (conn != null) {
conn.disconnect();
}
}
}
}
public int getFileRealSize(String url)
{
URL u = null;
try {
u = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpURLConnection connn = null;
try {
connn = (HttpURLConnection) u.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
int tempSize = connn.getContentLength();
return tempSize;
}
}
package com.nico;
import java.util.List;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.configue.Const;
import com.enetity.FileBean;
import com.util.HttpConn;
public class FileListView extends Activity {
private List<FileBean> list = null;
private String url = Const.url+"file.xml";
private ListView listview = null;
private ProgressDialog pd = null;
private String path;
private ProgressBar pb = null;
private Handler handler = new Handler() {
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
switch (msg.what) {
case 1:
list = new HttpConn().getData(url);
break;
case 2:
Intent intent = new Intent(FileListView.this,
DownLoadPage.class);
startActivity(intent);
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
pd = new ProgressDialog(this);
pd.setTitle("请等待");
pd.setMessage("正在跳转请稍后");
listview = (ListView) findViewById(R.id.listview);
new getListAsynTask().execute();
path = Environment.getExternalStorageDirectory().getAbsolutePath();
}
@Override
protected void onResume() {
super.onResume();
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView img = null;
TextView txt = null;
Button btn = null;
if (convertView == null) {
convertView = FileListView.this.getLayoutInflater().inflate(
R.layout.item, null);
}
img = (ImageView) convertView.findViewById(R.id.fileimg);
txt = (TextView) convertView.findViewById(R.id.filename);
btn = (Button) convertView.findViewById(R.id.downbtn);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(Const.isNotLoad(Environment
.getExternalStorageDirectory().getAbsolutePath(), Const.url+"test.rar"))
{
// Intent intent = new Intent(FileListView.this,
// DownLoadPage.class);
// startActivity(intent);
handler.sendEmptyMessage(2);
}
else
{
Toast.makeText(FileListView.this, "文件已下载", 5000).show();
}
}
});
return convertView;
}
}
public class getListAsynTask extends
AsyncTask<String, String, List<FileBean>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pd.show();
}
@Override
protected void onPostExecute(List<FileBean> result) {
super.onPostExecute(result);
listview.setAdapter(new MyAdapter());
pd.dismiss();
}
@Override
protected List<FileBean> doInBackground(String... params) {
list = new HttpConn().getData(url);
return list;
}
}
}
分享到:
相关推荐
`AndroidDownload`可能是一个包含实现断点续传功能的源代码的项目或库。这个项目的源码可以作为学习和参考的资源,帮助开发者更深入地理解和实践断点续传技术在Android环境中的应用。 总之,Android的断点续传技术...
同时,为了支持断点续传,我们需要检查本地是否存在已下载的部分,并获取其大小。 ```java public class DownloadTask { private String url; private String filePath; private long downloadedSize; // 构造...
当文件存储在云服务(如Amazon S3、Google Cloud Storage或阿里云OSS)时,这些服务通常支持断点续传API,需要按照它们的文档进行操作。 6. 进度更新与UI交互: 使用Handler或AsyncTask更新UI,显示下载进度。...
在这个公司项目的第一个版本中,我们关注的是如何实现一个功能完善的FTP上传系统,该系统支持断点续传并能够实时显示上传进度。 FTP(File Transfer Protocol)是一种用于在网络上进行文件传输的标准协议。它允许...
本"oss下载(断点续传,后台显示进度)源代码.rar"压缩包包含了一个实现阿里云OSS断点续传下载并带有进度条展示的示例代码。通过分析和学习这个代码,我们可以掌握以下几个关键知识点: 1. **阿里云OSS SDK**:首先...
本文将深入探讨如何在Android应用中实现在弹出对话框中强制用户更新,并且支持断点续传技术,提高下载效率。 首先,我们要理解强制更新的基本流程。当用户打开应用时,应用会通过网络请求检查服务器上的最新版本...
Android断点续传及网格文件下载的Java实例源代码,支持在暂停下载后继续下载,现在很多下载软件普遍都使用的功能,在Android手机软件中应用断点续传技术也是很有必要的。通过这个Android源码可学习一下如何基于Java...
一个适用于android环境的断点续传下载模块,支持暂停、继续。实例中通过几个下载wma音乐文件的例子,来演示android下载内容,并支持暂停、继续的功能,并可在意外断开的情况下,下次打开后继续下载,也就是断点续传...
本文将深入探讨这两个概念,并结合"android 多线程下载源码(支持断点续传)"这一主题,分析相关知识点。 1. **多线程下载** 多线程下载是为了提高下载速度,通过同时开启多个下载任务,让每个任务负责一部分文件...
Android提供了`DownloadManager`类,它是一个系统服务,可以处理后台下载任务,并支持断点续传。通过`ACTION_DOWNLOAD_COMPLETE`广播接收器,开发者可以监控下载状态。在创建下载请求时,可以设置`DownloadManager....
如果是206 Partial Content,说明服务器支持断点续传并返回了部分数据。如果不是206,可能是服务器不支持断点续传,这时可能需要重新开始下载。 5. **合并文件**:将接收到的数据追加到本地已有的文件中。如果这是...
3. **处理响应**:服务器收到请求后,如果支持断点续传,会返回状态码206(Partial Content),并在响应头中包含`Content-Range`,说明返回的数据范围。我们需要检查这个信息,确保服务器按预期返回了数据。 4. **...
本示例"Android文件下载Demo(可断点续传)"提供了一个实现多线程下载并且支持断点续传功能的源代码DEMO。下面将详细解释这个Demo中涉及的关键知识点。 1. **多线程下载**: 在Android中,为了提高文件下载速度,...
项目的源代码可能展示了如何集成以上组件,以及如何在Android应用程序中有效地实现多线程断点续传的流程。通过学习该项目,开发者可以了解到如何在实际应用中实现高效且用户体验良好的文件下载功能。 总之,...
总之,构建一个支持在线播放和断点续传的Android音乐播放器需要对Android的多媒体框架、网络编程、数据存储以及多线程有深入的理解。通过实践和研究给定的源代码,开发者可以进一步提升自己在Android音视频开发领域...
安卓开发者可以利用第三方库简化断点续传的实现,例如“Volley”、“OkHttp”等网络库都提供了相关的支持。以“Android-Async-Http”为例,这个库内置了断点续传功能,开发者只需要配置好请求,框架会自动处理下载...
本项目“Android ftp断点续传Demo”旨在演示如何在Android应用中实现FTP客户端功能,支持断点续传,即在中断后能够从上次停止的地方继续传输文件,这对于大文件的传输尤其有用。以下将详细介绍该Demo涉及的关键知识...
4. **断点续传**:支持断点续传,即在网络中断或用户操作等原因导致上传暂停后,可以继续从上次中断的地方开始上传,避免了重新上传整个文件。 5. **多线程上传**:允许同时上传多个文件,通过多线程机制进一步提升...
Android multi-task file download engine. > 中文文档 DEMO Installation FileDownloader is installed by adding the following dependency to your build.gradle file: dependencies { compile '...