Android多线程断点下载jar包
根据网上寻找的资料,加上自己的整合,打包的一个Android多线程下载。可以实现断点下载,暂停继续取消下载,能够显示现在已完成大小,文件大小,完成度,下载速度等。使用方法如下:
public class MainActivity extends Activity implements OnClickListener,DownloadListener{
private static final String TAG = "MainActivity";
private static final String SD_PATH = Environment.getExternalStorageDirectory().getPath();
private boolean isPause = false;
private MultiThreadDownload multiThreadDownload ;
private Button buttonDownload;
private Button buttonCancel;
private TextProgressBar progressBarTest;//可以使用普通的进度,该进度条是可以显示现在完成度
private TextView textInfo;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//布局文件就是两个简单的按钮一个用于暂停继续下载,一个用于取消任务,还有一个文本用于显示已完成,文件大小,下载完成度,下载速度等信息以及一个进度条。
//http://d1.apk8.com:8020/game/plantsvszombies2.apk
buttonDownload = (Button)findViewById(R.id.buttonDownload);
buttonDownload.setText("单击下载");
buttonDownload.setOnClickListener(this);
progressBarTest = (TextProgressBar) findViewById(R.id.progressBarTest);
textInfo = (TextView) findViewById(R.id.textInfo);
buttonCancel = (Button) findViewById(R.id.buttonCancel);
buttonCancel.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String downloadUrl = "http://dldir1.qq.com/invc/qqpinyin/QQInput4.1_1133(1001).apk";
String savePath = SD_PATH+File.separator+"downloadHelper"+File.separator;
String fileName = "QQInput.apk";
switch(v.getId()){
case R.id.buttonDownload:
if(!isPause){
multiThreadDownload = new MultiThreadDownload(MainActivity.this,downloadUrl, savePath, fileName, this);
multiThreadDownload.start();
buttonDownload.setText("下载...");
isPause = true;
}else{
buttonDownload.setText("暂停...");
isPause = false;
multiThreadDownload.onPause();
multiThreadDownload = null;
}
break;
case R.id.buttonCancel:
if(multiThreadDownload==null){
multiThreadDownload = new MultiThreadDownload(MainActivity.this,downloadUrl, savePath, fileName, this);
}
Log.d(TAG, ""+multiThreadDownload.isDownload());
if(multiThreadDownload.isDownload()){
multiThreadDownload.onCancel();
isPause = false;
}
multiThreadDownload = null;
break;
}
}
private Handler multiHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what==1){
Bundle data = msg.getData();
float downloadSpeed = data.getFloat("downloadSpeed");
boolean speedHigh = false;
if(downloadSpeed>500){
speedHigh = true;
downloadSpeed/=1024;
}
progressBarTest.setProgress(data.getInt("completedSize"));
textInfo.setText(String.format("已完成:%.2fMB", data.getInt("completedSize")/1024f/1024)+"/"+
String.format("总大小:%.2fMB", data.getInt("fileSize")/1024f/1024)+"\n"+
String.format("进度:%.2f%%", data.getBoolean("isCompleted")?100.00:data.getFloat("downloadPercent"))+"/"+
String.format(speedHigh?"速度:%.2fM/S":"速度:%.2fKB/S", downloadSpeed));
if(data.getBoolean("isCompleted")){
buttonDownload.setText("下载完成");
textInfo.setText("下载完成");
}
}
}
};
@Override
public void onDownloading(boolean isCompleted,boolean isPause,boolean isCancel,int fileSize,int completedSize,int downloadedSize,float downloadPercent, float downloadSpeed) {
Log.d("MainActivity", isCompleted+"||"+isPause+"||"+completedSize+"||"+downloadedSize+"||"+downloadPercent+"||"+downloadSpeed);
Message message = Message.obtain();
message.what = 1;
Bundle data = new Bundle();
data.putBoolean("isCancel", isCancel);
data.putBoolean("isCompleted", isCompleted);
data.putInt("fileSize", fileSize);
data.putInt("completedSize", completedSize);
data.putInt("downloadedSize", downloadedSize);
data.putFloat("downloadPercent", downloadPercent);
data.putFloat("downloadSpeed", downloadSpeed);
message.setData(data);
multiHandler.sendMessage(message);
}
@Override
public void onBeforeDownload(boolean isCompleted,boolean isPause,boolean isCancel,int fileSize){
progressBarTest.setMax(fileSize);
}
@Override
public void onAfterDownload(boolean isCompleted,boolean isPause,boolean isCancel,int fileSize){
}
@Override
public void onPause(boolean isCompelted,boolean isPause,boolean isCancel,int fileSize,int completedSize,int downloadedSize,float downloadPercent,float downloadSpeed){
textInfo.setText("暂停...");
Log.d(TAG, "暂停...");
}
@Override
public void onCancelDownload(){
progressBarTest.setProgress(0);
textInfo.setText("下载取消");
buttonDownload.setText("重新下载");
}
}
Android(Fragment整合上篇Webservice后台)
实现使用Android作为客户端请求Webservice开发的服务端交换登录,使用Fragment方便开发,并带有滑动效果,需结合上篇Webservice作为后台才能正常运行。
WebService+CXF+Spring+MySql+注解
为了整合Android后台而开发的使用Webservice的服务器端,导入MySQL数据库文件运行test中的类可以使用,在Android端如何使用请看下篇Android端的项目
基于SQLiteOpenHelper封装的SQLite操作jar包
在原有的SQLiteOpenHelper基础上进行的封装,更加方便使用,常用的增删改查方法无需在重新覆写,内文档说明如何使用,只需阅读一次终生受益。
在线考试系统
基于JavaEE在线考试系统,用tomcat发布,mysql作为后台数据库。
C语言编写的职工信息管理系统
C语言职工管理信息系统是用于职工的日常信息进行管理的操作