package com.updateapp;
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.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class UpdateAppActivity extends Activity {
/** Called when the activity is first created. */
private static final String TAG = "Update";
private Button btnUpdateApp;
private ProgressDialog pBar;
private String downPath = "http://10.0.2.2:8080/";
private String appName = "NewAppSample.apk";
private String appVersion = "version.json";
private int newVerCode = 0;
private String newVerName = "";
private Handler handler=new Handler();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
if(isNetworkAvailable(this) == false){
return;
}else{
checkToUpdate();
}
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
btnUpdateApp = (Button)findViewById(R.id.btnUpdateApp);
btnUpdateApp.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
//check the Network is available
private static boolean isNetworkAvailable(Context context) {
// TODO Auto-generated method stub
try{
ConnectivityManager cm = (ConnectivityManager)context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netWorkInfo = cm.getActiveNetworkInfo();
return (netWorkInfo != null && netWorkInfo.isAvailable());//检测网络是否可用
}catch(Exception e){
e.printStackTrace();
return false;
}
}
//check new version and update
private void checkToUpdate() throws NameNotFoundException {
// TODO Auto-generated method stub
if(getServerVersion()){
int currentCode = CurrentVersion.getVerCode(this);
if(newVerCode > currentCode)
{//Current Version is old
//弹出更新提示对话框
showUpdateDialog();
}
}
}
//show Update Dialog
private void showUpdateDialog() throws NameNotFoundException {
// TODO Auto-generated method stub
StringBuffer sb = new StringBuffer();
sb.append("当前版本:");
sb.append(CurrentVersion.getVerName(this));
sb.append("VerCode:");
sb.append(CurrentVersion.getVerCode(this));
sb.append("\n");
sb.append("发现新版本:");
sb.append(newVerName);
sb.append("NewVerCode:");
sb.append(newVerCode);
sb.append("\n");
sb.append("是否更新?");
Dialog dialog = new AlertDialog.Builder(UpdateAppActivity.this)
.setTitle("软件更新")
.setMessage(sb.toString())
.setPositiveButton("更新", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
showProgressBar();//更新当前版本
}
})
.setNegativeButton("暂不更新", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create();
dialog.show();
}
protected void showProgressBar() {
// TODO Auto-generated method stub
pBar = new ProgressDialog(UpdateAppActivity.this);
pBar.setTitle("正在下载");
pBar.setMessage("请稍后...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downAppFile(downPath + appName);
}
//Get ServerVersion from GetUpdateInfo.getUpdateVerJSON
private boolean getServerVersion() {
// TODO Auto-generated method stub
try{
String newVerJSON = GetUpdateInfo.getUpdataVerJSON(downPath + appVersion);
JSONArray jsonArray = new JSONArray(newVerJSON);
if(jsonArray.length() > 0){
JSONObject obj = jsonArray.getJSONObject(0);
try{
newVerCode = Integer.parseInt(obj.getString("verCode"));
newVerName = obj.getString("verName");
}catch(Exception e){
Log.e(TAG, e.getMessage());
newVerCode = -1;
newVerName = "";
return false;
}
}
}catch(Exception e){
Log.e(TAG, e.getMessage());
return false;
}
return true;
}
protected void downAppFile(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();
Log.isLoggable("DownTag", (int) length);
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if(is == null){
throw new RuntimeException("isStream is null");
}
File file = new File(Environment.getExternalStorageDirectory(),appName);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
do{
ch = is.read(buf);
if(ch <= 0)break;
fileOutputStream.write(buf, 0, ch);
}while(true);
is.close();
fileOutputStream.close();
haveDownLoad();
}catch(ClientProtocolException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
}.start();
}
//cancel progressBar and start new App
protected void haveDownLoad() {
// TODO Auto-generated method stub
handler.post(new Runnable(){
public void run(){
pBar.cancel();
//弹出警告框 提示是否安装新的版本
Dialog installDialog = new AlertDialog.Builder(UpdateAppActivity.this)
.setTitle("下载完成")
.setMessage("是否安装新的应用")
.setPositiveButton("确定", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
installNewApk();
finish();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
})
.create();
installDialog.show();
}
});
}
//安装新的应用
protected void installNewApk() {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(
new File(Environment.getExternalStorageDirectory(),appName)),
"application/vnd.android.package-archive");
startActivity(intent);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Android系统单个应用的自动升级更新(通过http连接tomcat7)
共38个文件
class:17个
png:6个
java:5个
5星 · 超过95%的资源 需积分: 50 1.4k 下载量 44 浏览量
2012-02-06
11:01:21
上传
评论 18
收藏 942KB RAR 举报
温馨提示
android系统应用的自动升级更新,通过http连接tomcat7下载并安装。
资源推荐
资源详情
资源评论
收起资源包目录
UpdateApp.rar (38个子文件)
UpdateApp
.project 812B
project.properties 360B
proguard.cfg 1KB
src
com
updateapp
ReplaceBroadcastReceiver.java 627B
CurrentVersion.java 947B
UpdateAppActivity.java 7KB
GetUpdateInfo.java 1KB
AndroidManifest.xml 1KB
res
drawable-ldpi
ic_launcher.png 116KB
drawable-hdpi
ic_launcher.png 116KB
drawable-mdpi
ic_launcher.png 116KB
layout
main.xml 576B
values
strings.xml 239B
assets
gen
com
updateapp
R.java 770B
.classpath 356B
bin
resources.ap_ 335KB
UpdateApp.apk 343KB
classes
com
updateapp
R$layout.class 379B
CurrentVersion.class 2KB
UpdateAppActivity$5$1.class 1KB
UpdateAppActivity.class 7KB
R$drawable.class 392B
R$attr.class 325B
UpdateAppActivity$1.class 777B
UpdateAppActivity$5$2.class 1010B
GetUpdateInfo.class 2KB
UpdateAppActivity$2.class 880B
R$id.class 375B
ReplaceBroadcastReceiver.class 1KB
UpdateAppActivity$5.class 2KB
UpdateAppActivity$3.class 841B
R.class 496B
R$string.class 444B
UpdateAppActivity$4.class 3KB
res
drawable-ldpi
ic_launcher.png 110KB
drawable-hdpi
ic_launcher.png 110KB
drawable-mdpi
ic_launcher.png 110KB
classes.dex 13KB
共 38 条
- 1
快上车_来不及解释了
- 粉丝: 308
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页