package jtapp.updateapksamples;
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 org.json.JSONArray;
import org.json.JSONObject;
import android.app.Activity;
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;
import android.util.Log;
public class UpdateActivity extends Activity {
private static final String TAG = "Update";
public ProgressDialog pBar;
private Handler handler = new Handler();
private int newVerCode = 0;
private String newVerName = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (getServerVerCode()) {
int vercode = Config.getVerCode(this);
if (newVerCode > vercode) {
doNewVersionUpdate();
} else {
notNewVersionShow();
}
}
}
private boolean getServerVerCode() {
try {
String verjson = NetworkTool.getContent(Config.UPDATE_SERVER
+ Config.UPDATE_VERJSON);
JSONArray array = new JSONArray(verjson);
if (array.length() > 0) {
JSONObject obj = array.getJSONObject(0);
try {
newVerCode = Integer.parseInt(obj.getString("verCode"));
newVerName = obj.getString("verName");
} catch (Exception e) {
newVerCode = -1;
newVerName = "";
return false;
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return false;
}
return true;
}
private void notNewVersionShow() {
int verCode = Config.getVerCode(this);
String verName = Config.getVerName(this);
StringBuffer sb = new StringBuffer();
sb.append("当前版本:");
sb.append(verName);
sb.append(" Code:");
sb.append(verCode);
sb.append(",\n已是最新版,无需更新!");
Dialog dialog = new AlertDialog.Builder(UpdateActivity.this)
.setTitle("软件更新").setMessage(sb.toString())// 设置内容
.setPositiveButton("确定",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
}
private void doNewVersionUpdate() {
int verCode = Config.getVerCode(this);
String verName = Config.getVerName(this);
StringBuffer sb = new StringBuffer();
sb.append("当前版本:");
sb.append(verName);
sb.append(" Code:");
sb.append(verCode);
sb.append(", 发现新版本:");
sb.append(newVerName);
sb.append(" Code:");
sb.append(newVerCode);
sb.append(", 是否更新?");
Dialog dialog = new AlertDialog.Builder(UpdateActivity.this)
.setTitle("软件更新")
.setMessage(sb.toString())
// 设置内容
.setPositiveButton("更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(UpdateActivity.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(Config.UPDATE_SERVER
+ Config.UPDATE_APKNAME);
}
})
.setNegativeButton("暂不更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 点击"取消"按钮之后退出程序
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
}
void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
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(),
Config.UPDATE_SAVENAME);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
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(Environment
.getExternalStorageDirectory(), Config.UPDATE_SAVENAME)),
"application/vnd.android.package-archive");
startActivity(intent);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android手机应用程序自动更新源码
共114个文件
svn-base:31个
all-wcprops:29个
entries:29个
4星 · 超过85%的资源 需积分: 21 17 下载量 41 浏览量
2012-09-30
12:49:07
上传
评论
收藏 129KB RAR 举报
温馨提示
我们看到很多Android应用都具有自动更新功能,用户一键就可以完成软件的升级更新。得益于Android系统的软件包管理和安装机制。
资源推荐
资源详情
资源评论
收起资源包目录
Android手机应用程序自动更新源码 (114个子文件)
all-wcprops 672B
all-wcprops 636B
all-wcprops 558B
all-wcprops 375B
all-wcprops 230B
all-wcprops 230B
all-wcprops 230B
all-wcprops 222B
all-wcprops 218B
all-wcprops 218B
all-wcprops 218B
all-wcprops 216B
all-wcprops 210B
all-wcprops 204B
all-wcprops 115B
all-wcprops 103B
all-wcprops 98B
all-wcprops 98B
all-wcprops 95B
all-wcprops 92B
all-wcprops 92B
all-wcprops 92B
all-wcprops 92B
all-wcprops 92B
all-wcprops 89B
all-wcprops 86B
all-wcprops 86B
all-wcprops 86B
all-wcprops 62B
proguard.cfg 1KB
proguard.cfg 1KB
.classpath 280B
.classpath 280B
entries 1KB
entries 1KB
entries 732B
entries 558B
entries 400B
entries 400B
entries 400B
entries 394B
entries 394B
entries 394B
entries 386B
entries 383B
entries 380B
entries 378B
entries 308B
entries 302B
entries 251B
entries 251B
entries 250B
entries 245B
entries 239B
entries 239B
entries 234B
entries 234B
entries 233B
entries 228B
entries 228B
entries 225B
entries 219B
UpdateActivity.java 5KB
CallAndSms.java 3KB
NetworkTool.java 1KB
Config.java 1KB
ReceiverSMS.java 1KB
icon.png 4KB
icon.png 4KB
icon.png 3KB
icon.png 3KB
icon.png 2KB
icon.png 2KB
.project 861B
.project 855B
default.properties 362B
default.properties 362B
UpdateActivity.java.svn-base 5KB
icon.png.svn-base 4KB
icon.png.svn-base 4KB
CallAndSms.java.svn-base 3KB
icon.png.svn-base 3KB
icon.png.svn-base 3KB
icon.png.svn-base 2KB
icon.png.svn-base 2KB
NetworkTool.java.svn-base 1KB
main.xml.svn-base 1KB
Config.java.svn-base 1KB
ReceiverSMS.java.svn-base 1KB
proguard.cfg.svn-base 1KB
proguard.cfg.svn-base 1KB
AndroidManifest.xml.svn-base 1001B
.project.svn-base 861B
.project.svn-base 855B
AndroidManifest.xml.svn-base 754B
main.xml.svn-base 382B
default.properties.svn-base 362B
default.properties.svn-base 362B
.classpath.svn-base 280B
.classpath.svn-base 280B
共 114 条
- 1
- 2
资源评论
- vpei20012012-09-30这个程序不好,打开错误。
- ceso1242013-09-09可以用,值得借鉴
- 25640636222013-09-09很好的资源,可以实现软件的自动更新
- 云哥活动室2014-05-05差不多可以用,不全面,I整体还可以
- liaowangliang2013-03-31差不多可以用,不全面,I整体还可以
CodeArtisanX
- 粉丝: 382
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功