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;
}
}
}
分享到:
相关推荐
Android断点续传及网格文件下载的Java实例源代码,支持在暂停下载后继续下载,现在很多下载软件普遍都使用的功能,在Android手机软件中应用断点续传技术也是很有必要的。通过这个Android源码可学习一下如何基于Java...
一个适用于android环境的断点续传下载模块,支持暂停、继续。实例中通过几个下载wma音乐文件的例子,来演示android下载内容,并支持暂停、继续的功能,并可在意外断开的情况下,下次打开后继续下载,也就是断点续传...
阿里云oss断点续传下载(带进度条)示例代码,稍微改下可做成自己需要的下载工具。地址可配置。欢迎大家下载评论
Android multi-task file download engine. > 中文文档 DEMO Installation FileDownloader is installed by adding the following dependency to your build.gradle file: dependencies { compile '...
android 断点续传下载,支持多任务,代码下载可以直接使用
Android断点续传及网格文件下载的Java实例源代码,支持在暂停下载后继续下载,现在很多下载软件普遍都使用的功能,在Android手机软件中应用断点续传技术也是很有必要的。通过这个Android源码可学习一下如何基于Java...
断点续传和网络mp3播放,支持下载和在线播放 不多说,上源码
1、使用HttpClient发送GET请求: 2、使用HttpClient发送POST请求:和之前的GET类似 3、使用异步HttpClient发送请求,可以在...7、在gitbub上面下载已经有支持断点续传功能的代码HttpUtils 8、源码关联-技巧,非常重要:
支持断点续传下载 支持自定义文件夹路径、文件名。 支持自定义SQLiteOpenHelper,用于保存下载进度等 下载进度回调时自动切换到UI线程,方便更新UI。 自动与activity绑定生命周期,无需手动释放 act
Android项目源码类似于360云盘的网盘客户端支持断点续传是一个基于安卓的网盘客户端项目源码,因为以前的客户端使用的是花生壳现在花生壳网址失效了,没法登陆和演示了。翻了翻源码貌似实现了很多功能,都是纯java,...
Android 网络通信组件;支持get,post,下载,断点续传,上传;支持全局缓存,各个接口不同缓存策略;使用方式简单;轻量级代码,结构清晰;基于OKHTTP3;数据库使用GreenDao
* ldown是一款跨平台多任务的多线程断点续传的下载软件, * 支持 HTTP / FTP / BT / Magnet 磁力链接等类型的文件下载;支持 Win、android、Linux系统。 * * 欢迎联系:3603233570@qq.com * 注意:演示...
最近在做一个下载文件的功能的时候,因为要支持断点续传,虽然整体上思路很清晰,也比较简单,但是在做的过程中还是遇到了几个坑,特意在此记录一下,也重新梳理下下载断点续传的实现过程。 一、要注意的地方 这里先...
License ...GigaGet is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the ...
翻了翻源码貌似实现了很多功能,都是纯java,没有封装,慢慢捋代码的话应该能捋出些有用的东西,本文配图是在文档里面扒出来了,凑合着看吧,主要看看有啥功能。这里有个服务端的文档...
主要为大家详细介绍了Android多线程断点续传下载功能的实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
支持文件FTP断点续传上传 支持FTPS/SFTP断点续传,see 支持https地址下载 在配置文件中很容易就可以设置CA证书的信息 支持多线程分块下载,能更有效的发挥机器IO性能 支持300、301、302重定向下载链接下载 下载...
2.支持断点。 使用多线程的好处:使用多线程下载会提升文件下载的速度。那么多线程下载文件的过程是: (1)首先获得下载文件的长度,然后设置本地文件的长度。 HttpURLConnection.getContentLength();//获取下载...
一个操作简单、支持断点续传、安全性能较高的安卓应用更新库 特点 步骤简单,几行代码就搞定 用户实现是否更新接口,有助于用户自定义 断点续传,断网后,当启动网络后继续下载 安全,代码操作进行大量捕获异常,...