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).zip项目安卓应用源码下载Android应用程序的自动更新升级(自身升级、通过tomcat).zip项目安卓应用源码下载 1.适合学生毕业设计研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
资源推荐
资源详情
资源评论
收起资源包目录
Android应用程序的自动更新升级(自身升级、通过tomcat).zip (38个子文件)
Android应用程序的自动更新升级(自身升级、通过tomcat)
Android应用程序的自动更新升级(自身升级、通过tomcat)
UpdateApp
.project 812B
bin
UpdateApp.apk 343KB
res
drawable-ldpi
ic_launcher.png 110KB
drawable-hdpi
ic_launcher.png 110KB
drawable-mdpi
ic_launcher.png 110KB
resources.ap_ 335KB
classes
com
updateapp
R$string.class 444B
R$layout.class 379B
UpdateAppActivity$5$2.class 1010B
UpdateAppActivity.class 7KB
CurrentVersion.class 2KB
UpdateAppActivity$2.class 880B
R$drawable.class 392B
UpdateAppActivity$1.class 777B
R.class 496B
UpdateAppActivity$4.class 3KB
R$attr.class 325B
UpdateAppActivity$5.class 2KB
UpdateAppActivity$5$1.class 1KB
ReplaceBroadcastReceiver.class 1KB
R$id.class 375B
GetUpdateInfo.class 2KB
UpdateAppActivity$3.class 841B
classes.dex 13KB
proguard.cfg 1KB
AndroidManifest.xml 1KB
src
com
updateapp
UpdateAppActivity.java 7KB
CurrentVersion.java 947B
ReplaceBroadcastReceiver.java 627B
GetUpdateInfo.java 1KB
project.properties 360B
res
values
strings.xml 239B
layout
main.xml 576B
drawable-ldpi
ic_launcher.png 116KB
drawable-hdpi
ic_launcher.png 116KB
drawable-mdpi
ic_launcher.png 116KB
.classpath 356B
gen
com
updateapp
R.java 770B
共 38 条
- 1
资源评论
yxkfw
- 粉丝: 76
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
- 基于MIC+NE555光敏电阻的声光控电路Multisim仿真原理图
- 基于JSP毕业设计-基于WEB操作系统课程教学网站的设计与实现(源代码+论文).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功