Android2.3.3系统开发一个在线OTA功能下载文件功能
来源:互联网 发布:中国facebook知乎 编辑:程序博客网 时间:2024/06/06 14:15
最近一直在搞公司分配给我的个人的一个小程序。搞了一段时间终于搞好了。
首先,是在服务器搞了一个services.xml
<?xml version="1.0" encoding="UTF-8"?>
<info>
<version>
TELWORX-V1.4
</version>
<url>
http://127.0.0.1:8080/update/data/update.zip
</url>
<description>
Versions: TELWORX-V1.4
</description>
</info>
第二步是要去服务器找services.xml获取里面的version.
url = new URL("http://127.0.0.1:8080/update/data/update.xml");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
updateInfo = ParseXmlUtils.parseXml(connection.getInputStream());
第三步就是去解析这个xml
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 解析Xml文件
* @author andrew
* 2012-04-05
* ParseXmlUtils.java
*/
public class ParseXmlUtils {
/**
* 通过InputStream 解析文件
* @param in
* @return
*/
public static UpdateInfo parseXml(InputStream in){
UpdateInfo updateInfo = new UpdateInfo();
/*InputStream 也可以是网络,或者文件*/
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
/*以树状格式,存储在内存中;比较耗费内存*/
Document doc = db.parse(in);
Element root = doc.getDocumentElement();
/*得到update的节点列表*/
NodeList resultNode = root.getElementsByTagName("info");
for(int i = 0; i < resultNode.getLength();i++){
Element res = (Element)resultNode.item(i);
updateInfo.setVersion(res.getElementsByTagName("version").item(0).getFirstChild().getNodeValue());
updateInfo.setUrl(res.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
updateInfo.setDescription(res.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return updateInfo;
}
}
第四步比较当前系统的版本号跟在services.xml里面得到的版本号是否一致。如果不一致就下载服务器的文件,如果一致就提醒用户。
/**
* 比较版本是否相同 相同就提示"版本一样 不需要更新"
*/
if(updateInfo.getVersion().trim().equals(VersionUtil.getVersionName())){
Toast.makeText(this, R.string.versions, Toast.LENGTH_SHORT).show();
}else{
download.showUpdateDialog(updateInfo);
}
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 android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
/**
* 下载
* @author andrew
* 2012-04-06
*
*/
public class Download extends Activity{
private Context mContext;
private Dialog downloadDialog;
private static final String savePath = "/sdcard/sub_sda1/";
private static final String saveFileName = savePath + "update.zip";
private ProgressBar mProgress;
private static final int DOWN_UPDATE = 1;
private static final int DOWN_OVER = 2;
private int progress;
private Thread downLoadThread;
private boolean interceptFlag = false;
UpdateInfo updateInfos;
/**
* 初始化
* @param context
*/
public Download(Context context) {
this.mContext = context;
}
/**
* 判断是否要书写进度条
*/
private Handler mHandler = new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case DOWN_UPDATE:
mProgress.setProgress(progress);
break;
case DOWN_OVER:
updateSystem();
break;
default:
break;
}
};
};
/**
* 版本不一样 就提示是否需要下载新的版本
* @param updateInfo
*/
public void showUpdateDialog(UpdateInfo updateInfo) {
updateInfos = updateInfo;
AlertDialog alertDialog = new AlertDialog.Builder(mContext)
.setTitle(R.string.tiptitle)
.setMessage(updateInfo.getDescription())
.setPositiveButton(R.string.yes,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
showDownloadDialog();
}
})
.setNegativeButton(R.string.cleck,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertDialog.show();
}
/**
* 显示下载进度条
* 按取消按键可以取消下载
*/
private void showDownloadDialog(){
AlertDialog.Builder builder = new Builder(mContext);
builder.setTitle(R.string.system);
final LayoutInflater inflater = LayoutInflater.from(mContext);
View v = inflater.inflate(R.layout.progress, null);
mProgress = (ProgressBar)v.findViewById(R.id.progress);
builder.setView(v);
builder.setNegativeButton(R.string.cleck, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
interceptFlag = true;
}
});
downloadDialog = builder.create();
downloadDialog.show();
updateSystem();
// download();
}
/**
* 下载更新系统文件
* @param url
*/
private void download(){
downLoadThread = new Thread(mdownApkRunnable);
downLoadThread.start();
}
/**
* 利用线程下载更新文件
*/
private Runnable mdownApkRunnable = new Runnable() {
@Override
public void run() {
try {
URL url = new URL(updateInfos.getUrl().trim());
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.connect();
int length = conn.getContentLength();
InputStream is = conn.getInputStream();
File file = new File(savePath);
if(!file.exists()){
file.mkdir();
}
String apkFile = saveFileName;
File ApkFile = new File(apkFile);
FileOutputStream fos = new FileOutputStream(ApkFile);
int count = 0;
byte buf[] = new byte[1024];
try{
do{
int numread = is.read(buf);
count += numread;
progress =(int)(((float)count / length) * 100);
//更新进度
mHandler.sendEmptyMessage(DOWN_UPDATE);
fos.write(buf,0,numread);
}while(!interceptFlag);//点击取消就停止下载.
}catch(Exception e){
e.printStackTrace();
}finally{
Message message2=new Message();
message2.what=2;
mHandler.sendMessage(message2);
//下载完成后关闭下载进度条
downloadDialog.dismiss();
}
fos.close();
is.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}
}
};
/**
* 更新系统
* 按确定按钮就可以更新系统
* 按取消按钮就可以取消更新系统
*/
private void updateSystem(){
AlertDialog updateDialog = new AlertDialog.Builder(mContext)
.setTitle(R.string.tishi)
.setMessage(R.string.updatesystem)
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
updateDialog.show();
}
}
- Android2.3.3系统开发一个在线OTA功能下载文件功能
- JAVA开发常用功能二--文件下载
- (android之系统)android OTA 的功能实现和修改
- 实现文件下载功能
- 文件下载功能
- 文件下载功能
- 实现文件下载功能
- php文件下载功能
- 文件下载功能
- 文件下载功能
- 文件下载功能
- 文件下载小功能
- PHP文件下载功能
- js下载文件功能
- Swing文件下载功能
- 文件下载功能
- PHP 文件下载功能
- 文件下载功能
- 设计模式 (三)抽象工厂模式(AbstractFactory)
- flex入门学习之7 DateField
- VS2010 代码前出现虚线的问题
- 关于eclipse没有GBK选项
- spring 框架的xml文件如何读取properties文件数据
- Android2.3.3系统开发一个在线OTA功能下载文件功能
- Oracle10g JDBC ojdbc14 DATE类型hibernate查询时分秒问题(纠结困扰了半天,汗)
- 介绍了8种非常YD用源代码去调试DLL的方法
- Struts 框架中的时间问题
- JS刷新当前页面
- 数据文件自动扩展只能到32G?
- postgreSQL hot stand by
- 进程通信 共享数据段
- android menu