package com.cloud.update;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
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.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
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.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
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 Config config;
private static String packgeName;
private int newVerCode = 0;
private String newVerName = "";
private int newSize = 0;
String UPDATE_SERVER = "http://ard.down.ttpod.com/release/android/";
String UPDATE_APKNAME = "TTPod_Android_V6.3.0.apk";
String UPDATE_VERJSON = "ver.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
packgeName = this.getPackageName();
config = new Config(UPDATE_SERVER, UPDATE_APKNAME, UPDATE_VERJSON);
if (getServerVerCode()) {
int vercode = UpdateActivity.getVerCode(this);
if (newVerCode > vercode) {
doNewVersionUpdate();
} else {
notNewVersionShow();
}
}
}
private boolean getServerVerCode() {
try {
String verjson = 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("versionCode"));
newVerName = obj.getString("versionName");
newSize = Integer.parseInt(obj.getString("apkSize"));
} catch (Exception e) {
newVerCode = -1;
newVerName = "";
newSize = -1;
return false;
}
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
private void notNewVersionShow() {
int verCode = UpdateActivity.getVerCode(this);
String verName = UpdateActivity.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() {
public void onClick(DialogInterface dialog,
int which) {
finish();
}
}).create();// 创建
// 显示对话框
dialog.show();
}
private void doNewVersionUpdate() {
int verCode = UpdateActivity.getVerCode(this);
String verName = UpdateActivity.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)
.setIcon(R.drawable.icon)
.setTitle("软件更新")
.setMessage(sb.toString())
// 设置内容
.setPositiveButton("更新",// 设置确定按钮
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(UpdateActivity.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
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 destDir = new File(
Environment.getExternalStorageDirectory(),
"angool/download");
if (!destDir.exists()) {
destDir.mkdirs();
}
File file = new File(
Environment.getExternalStorageDirectory(),
"angool/download/" + config.UPDATE_APKNAME);
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;
int size = (int) (count / (newSize/(1024/10)));
System.out.println(size);
pBar.setProgress(size);
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(), "angool/download/"
+ config.UPDATE_APKNAME)),
"application/vnd.android.package-archive");
startActivity(intent);
}
public void setConfig(Config config) {
this.config = config;
}
public class Config {
public String UPDATE_SERVER;
public String UPDATE_APKNAME;
public String UPDATE_VERJSON;
public Config(String UPDATE_SERVER, String UPDATE_APKNAME,
String UPDATE_VERJSON) {
this.UPDATE_APKNAME = UPDATE_APKNAME;
this.UPDATE_SERVER = UPDATE_SERVER;
this.UPDATE_VERJSON = UPDATE_VERJSON;
}
public String getUPDATE_SERVER() {
return UPDATE_SERVER;
}
public void setUPDATE_SERVER(String uPDATESERVER) {
UPDATE_SERVER = uPDATESERVER;
}
public String getUPDATE_APKNAME() {
return UPDATE_APKNAME;
}
public void setUPDATE_APKNAME(String uPDATEAPKNAME) {
UPDATE_APKNAME = uPDATEAPKNAME;
}
public String getUPDATE_VERJSON() {
return UPDATE_VERJSON;
}
public void setUPDATE_VERJSON(String uPDATEVERJSON) {
UPDATE_VERJSON = uPDATEVERJSON;
}
}
public static int getVerCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(packgeName, 0).versionCode;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verCode;
}
public static String getVerName(Context context) {
String verName = "";
try {
verName = context.getPackageManager().getPackageInfo(packgeName, 0).versionName;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verName;
}
/**
* 获取网址内容
*
* @param url
* @return
* @throws Exception
*
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
APP_Update.rar (35个子文件)
testUpdate
.project 846B
project.properties 563B
proguard.cfg 1KB
src
com
cloud
update
UpdateActivity.java 9KB
AndroidManifest.xml 1021B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 3KB
layout
main.xml 382B
values
strings.xml 169B
.settings
org.eclipse.jdt.core.prefs 177B
assets
gen
com
cloud
update
BuildConfig.java 158B
R.java 638B
.classpath 475B
bin
resources.ap_ 11KB
classes
com
cloud
update
R$layout.class 388B
BuildConfig.class 341B
R$drawable.class 394B
R$attr.class 334B
UpdateActivity$5.class 756B
UpdateActivity$1.class 869B
UpdateActivity$2.class 2KB
R.class 477B
R$string.class 421B
UpdateActivity$4.class 3KB
UpdateActivity.class 8KB
UpdateActivity$3.class 876B
UpdateActivity$Config.class 1KB
testUpdate.apk 18KB
AndroidManifest.xml 1021B
dexedLibs
jarlist.cache 120B
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
drawable-mdpi
icon.png 2KB
classes.dex 12KB
共 35 条
- 1
cheun258
- 粉丝: 1
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
前往页