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应用程序的自动更新升级(自身升级、通过tomcat)-IT计算机-毕业设计.zip (38个子文件)
Android应用程序的自动更新升级(自身升级、通过tomcat)
UpdateApp
bin
UpdateApp.apk 343KB
classes.dex 13KB
res
drawable-ldpi
ic_launcher.png 110KB
drawable-hdpi
ic_launcher.png 110KB
drawable-mdpi
ic_launcher.png 110KB
classes
com
updateapp
UpdateAppActivity$4.class 3KB
R$layout.class 379B
ReplaceBroadcastReceiver.class 1KB
UpdateAppActivity$2.class 880B
R$id.class 375B
R$drawable.class 392B
UpdateAppActivity$5.class 2KB
CurrentVersion.class 2KB
R$attr.class 325B
R.class 496B
UpdateAppActivity$1.class 777B
R$string.class 444B
UpdateAppActivity$5$1.class 1KB
GetUpdateInfo.class 2KB
UpdateAppActivity$3.class 841B
UpdateAppActivity$5$2.class 1010B
UpdateAppActivity.class 7KB
resources.ap_ 335KB
res
drawable-ldpi
ic_launcher.png 116KB
drawable-hdpi
ic_launcher.png 116KB
values
strings.xml 239B
drawable-mdpi
ic_launcher.png 116KB
layout
main.xml 576B
gen
com
updateapp
R.java 770B
proguard.cfg 1KB
src
com
updateapp
GetUpdateInfo.java 1KB
ReplaceBroadcastReceiver.java 627B
CurrentVersion.java 947B
UpdateAppActivity.java 7KB
.project 812B
.classpath 356B
project.properties 360B
AndroidManifest.xml 1KB
共 38 条
- 1
资源评论
芝麻粒儿
- 粉丝: 5w+
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- VR开发的概要介绍与分析
- 自动驾驶定位系列教程七:点云畸变补偿.pdf
- HM2302D-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2302B-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- springboot学生考勤管理系统
- HM2302A-VB一款N-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2301-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- 数据可视化的概要介绍与分析
- HM2301E-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
- HM2301D-VB一款P-Channel沟道SOT23的MOSFET晶体管参数介绍与应用说明
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功