Android 应用自动更新代码
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
public class Update extends BaseActivity {
public ProgressDialog pBar;
private Handler handler = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.update);
Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("系统更新")
.setMessage("发现新版本,请更新!")// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(Update.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar
.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile("http://url:8765/OA.apk");
}
}).setNegativeButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
}
}).create();// 创建
// 显示对话框
dialog.show();
}
void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的url
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment
.getExternalStorageDirectory(), "OA.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}
void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/OA.apk")),
"application/vnd.android.package-archive");
startActivity(intent);
}
}
分享到:
相关推荐
自动更新版本
Git项目自动更新版本信息
可以在vs2010中自动更新程序的版本号,含有完整源代码
VS编译发布根据SVN自动更新版本号,方便追踪和查找问题对应的版本
基于谷歌源码编译的,不会自动更新,可用于自动化软件开发
阿里巴巴博客漫游器自动更新版本 阿里巴巴博客漫阿里巴巴博客漫游器自动更新版本游器自动更新版本阿里巴巴博客漫游器自动更新版本 开心小妹妹友情提供 http://kxxmm.bokee.com
c# 自动升级 自动更新 版本同步 【csdn资料集合】绝对值得 里面有7个资料集合都好用,绝对能帮你
文档中一步步教你如何实现 自动更新版本安装程序
Auto_Upgrade :自动更新版本软件
虽然现在cs的用途是有限的,主要和硬件衔接的时候cs还是比bs管用滴,如果客户端多的情况下,自动更新就显得重要了,附件是源码,自己修改过。
自动检测服务端软件版本,可以实时更新、定时更新、更新进度条,PYQT5的UI界面
Android apk发布到google play上检测google play的新版本,并且下载更新
文件版本 文件名称 文件是否运行配置文件 \ini\Monitor.ini 服务器监控端口 超时时间 \log\monitor.log 服务器端服务连接相关的日志信息 \log\tran.log 服务器端文件下载相关的日志信息 FileMonitor.exe 服务器...
集成docker打包配置,serviceWorker离线缓存(每次打包自动更新版本号,弹出更新网页提示),多页面路由配置,postcss根据rem,vwvh转换器配置,补全css前缀,自动加上favicon,多页面html打包,less,webpack常规各种...
Delphi 开发的程序自动更新控件, xe7-xe10.4 安装测试过, 使用简单, 源码, 可以自己改图片, 自定义升级
使用VS2008,Remoting传输更新文件
自动生成或更新文件版本信息 支持一下三种方式: 1. 对单个exe或dll进行修改 2. 对某个目录下的所有exe或dll进行修改 3. 生成包含版本信息的.h头文件
注意,自动更新版本号时,如果增量编译,有时vc编译器并没有马上感应到有更新,未必将最新的rc文件编译进来。而 rebuild all 时,肯定生效。 pe文件操作类,下载别人的源码,稍微修改一点点,感谢原作者。
vc每次编译时,自动递增版本号信息 自己再vs2005下试验过,没有问题 使用方法: 将modifyver.exe拷到系统目录下(例如windows下) 在vc的预生成事件中加上“modifyver.exe $(ProjectName).rc” 思路是修改rc文件,用...