android检查自动升级
好像大半年没发点啥了,也不知道自己瞎忙啥,闲下来发给最近的东东《安卓在线升级》
类已经封装好了,简单的调用就OK了。这里的数据交互,我是用.NET写的一个webservice来交互。废话也不说了,直接上代码了。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
package com.android.xt.dg;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 com.android.xt.BLL.DGWebServicesBLL;import com.android.xt.common.AppSettingInfo;import com.android.xt.common.Common;import com.xt.dialog.XTAlertDialog;import com.xt.dialog.XTProgressDialog;import android.app.Activity;import android.app.ProgressDialog;import android.content.Context;import android.content.DialogInterface;import android.content.Intent;import android.content.pm.PackageManager.NameNotFoundException;import android.graphics.Bitmap.Config;import android.net.Uri;import android.os.Bundle;import android.os.Environment;import android.os.Handler;import android.os.Message;public class CheckUpgrade { public Activity Act;//调用该类的Activity public String apkName= "dgapk.apk";//下载保存的APK名称 public XTProgressDialog pBar;//进度条 public CheckUpgrade(Activity act) { this.Act=act; } /************* *获取当前的apk版本代码 ********************/ public static int getVerCode(Context context) { int verCode = -1; try { //com.android.xt.dg 为程序的包名。 verCode = context.getPackageManager().getPackageInfo( "com.android.xt.dg", 0).versionCode; } catch (NameNotFoundException e) { } return verCode; } /************* *获取当前的apk版本名称 ********************/ public static String getVerName(Context context) { String verName = ""; try { //com.android.xt.dg 为程序的包名。 verName = context.getPackageManager().getPackageInfo( "com.android.xt.dg", 0).versionName; } catch (NameNotFoundException e) { } return verName; } /************* *检查版本是升级。 ********************/ public void GetVersionInfo() { //显示进度条(此处进度条为自定义的一个进度条,可以修改为原生或者你自己的进度条,也可以直接去掉) pBar = XTProgressDialog.createDialog(Act).setMessage("正在检查,请稍候..."); pBar.show(); try { //启动一个线程调用webservice读取版本信息 new Thread(new Runnable(){ public void run(){ try { DGWebServicesBLL bll=new DGWebServicesBLL(); String msg=bll.GetServerVersion(); Message message=new Message(); message.what=1; Bundle data=new Bundle(); data.putString("msg", msg); message.setData(data); //读取数据近些界面的处理 myHandler.sendMessage(message); } catch (Exception e) { e.printStackTrace(); } } }).start(); } catch (Exception e) { e.printStackTrace(); Common.Message(e.getMessage(), Act); } } Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { try { pBar.dismiss(); //关闭进度条 switch(msg.what) { case 1:{ //取得数据并处理,我返回的数据格式为json格式,格式示例如下 //{"result":"true","versionCode":"1","versionName":"1.0","downFile":"http://www.968610.com.cn/app/dgapp.apk"} Bundle data=msg.getData(); String resultMsg=data.getString("msg"); JSONObject result=new JSONObject(resultMsg); String jsonResult=""; jsonResult=result.getString("result"); if(jsonResult.equals("false")) { resultMsg=result.getString("msg"); } else if(jsonResult.equals("true")) { //获取服务器版本 String ServerCodeStr =result.getString("versionCode"); int ServerCode=Integer.parseInt(ServerCodeStr); //服务器版本名 String ServerName =result.getString("versionName"); String downFile =result.getString("downFile"); //获取本地版本信息 int localCode=getVerCode((Context)Act); String localName=getVerName((Context)Act); if (ServerCode > localCode) { doNewVersionUpdate(ServerName,localName,downFile); // 更新新版本 } else { notNewVersionShow(ServerName); // 提示当前为最新版本 } } else { resultMsg="读取出错."; } }break; } }catch(Exception ex){ Common.Message(ex.getMessage(), Act); } super.handleMessage(msg); } }; /****************** *提示无最新版本, ********************************/ private void notNewVersionShow(String ServerName) { StringBuffer sb = new StringBuffer(); sb.append("当前版本:"+ServerName); sb.append(",已是最新版,无需更新!"); //此处对话框也为自定义对话框, XTAlertDialog.Builder builder=new XTAlertDialog.Builder(Act); builder.setTitle("软件更新").setMessage(sb.toString()); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); builder.create().show(); } /****************** *更新新版本 ********************************/ private void doNewVersionUpdate(String ServerName,String localName,final String DownFile) { StringBuffer sb = new StringBuffer(); sb.append("当前版本:"+localName); sb.append(", 发现新版本:"+ServerName); sb.append(", 是否更新?"); XTAlertDialog.Builder builder=new XTAlertDialog.Builder(Act); builder.setTitle("软件更新").setMessage(sb.toString()); builder.setPositiveButton("现在更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) {// pBar = new XTProgressDialog(Act); // pBar.setTitle("正在下载"); // pBar.setMessage("请稍候..."); //pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER); pBar = XTProgressDialog.createDialog(Act).setMessage("正在下载,请稍候..."); pBar.show(); downFile(DownFile); } }); builder.setNegativeButton("暂不更新", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // 点击"取消"按钮之后退出程序 dialog.dismiss(); } }); builder.create().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(), 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; if (length > 0) { } } } fileOutputStream.flush(); if (fileOutputStream != null) { fileOutputStream.close(); } down(); //下载完成,并通知安装 } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }.start(); } void down() { myHandler.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(), apkName)), "application/vnd.android.package-archive"); Act.startActivity(intent); } } |
这就封装好了。记得调用webservices检查新版本的方法我就不发了。那个就是个读取数据的而已。
看看怎么调用吧。两句话搞定(其实应该是一句。)
|
1
2
3
4
5
|
public void btnCheckUpgrade_Click(View v){ CheckUpgrade cu=new CheckUpgrade(this); cu.GetVersionInfo();} |
不扯了,咱不是官场人士,废话不说,代码重要。。。。等有空继续发些其他的。
本文从百度空间搬家到博客园
android检查自动升级的更多相关文章
- android实现自动升级并安装打开
http://blog.csdn.net/wa991830558/article/details/41014673 这是一个比较简单的程序,但网上还是有很多人问起这个问题,并且回答的人,也没有完全回答 ...
- Android 一s个相对完整的自动升级功能实现代码
由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...
- Android 一个相对完整的自动升级功能实现代码
由于项目的需要最近做了一个关于Android自动升级的功能,下面将贴出Android手机客户端的完整代码.这段代码参考别的代码居多,由于不满足需求,所以自己仅仅改了一些需要变动的内容,其他功能都是按照 ...
- Ionic4.x、Cordova Android 检测应用版本号、服务器下载文件以及实现App自动升级、安装
Android App 升级执行流程 1.获取本地版本号 2.请求服务器获取服务器版本号 3.本地版本和服务器版本不一致提示升级,弹窗提示用户是否更新 4.用户确定升级,调用文件传输方法下载 apk ...
- Ionic实战 自动升级APP(Android版)
Ionic 框架介绍 Ionic是一个基于Angularjs.可以使用HTML5构建混合移动应用的用户界面框架,它自称为是"本地与HTML5的结合".该框架提供了很多基本的移动用户 ...
- Android 实现应用升级方案(暨第三方自动升级服务无法使用后的解决方案)
第三方推送升级服务不再靠谱: 以前在做Android开发的时候,在应用升级方面都是使用的第三方推送升级服务,但是目前因为一些非技术性的问题,一些第三方厂商不再提供自动升级服务,比如友盟,那么当第三方推 ...
- Android自动检测版本及自动升级
步骤: 1.检测当前版本的信息AndroidManifest.xml-->manifest-->android:versionName. 2.从服务器获取版本号(版本号存在于xml文件中) ...
- android开发 更新升级安装到一半自动闪退
如题:android开发 更新升级安装到一半自动闪退,,,解决办法,如下(红色为我新增的代码) /** * 安装APK文件 */ private void installApk( ...
- C# Xamarin For Android自动升级项目实战
一.课程介绍 “明人不说暗话,跟着阿笨一起玩Xamarin”,本次分享课程阿笨将带来大家一起学习Xamarin For Android系列<C# Xamarin For Android自动升级项 ...
随机推荐
- 关于新版SDK报错You need to use a Theme.AppCompat theme的两种解决办法
android的一个小问题: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme ( ...
- [LeetCode] Super Ugly Number (Medium)
Super Ugly Number 最后WA没做出来. typedef long long int64; #define MAXBOUND 10000000 class Solution { publ ...
- dump 验证实例恢复的起点和终点
什么时候会产生实例恢复呢?当你数据库服务器异常断电,重启数据库就会发生实例恢复.实例恢复是由数据库自动完成的,无须DBA的干涉.当然这里有个前提条件:数据文件. 在线日志文件.控制文件不得有损坏. 我 ...
- android开发板
element14-beaglebone-black http://www.embest-tech.cn/shop/star/element14-beaglebone-black-rev-c.html ...
- (转载)LINUX UNBUNTU10.04 下 搭建OC编译环境
(转载)http://blog.sina.com.cn/s/blog_833996210100rgl4.html 1安装 / install GNUstep on ubuntu 下面列出来的包是安装G ...
- LR测试工具性能指标详解
这篇文章将对LoadRunner测试工具的性能指标从以下三点进行详解. 第一点.Web资源分析是从服务器入手对Web服务器的性能分析. 1.Hits per Second "每秒点击次数&q ...
- vss搭建于操作
1.下载的vvs2005,下载后先安装在服务器上,反正就是下一步下一步就对了 安装完成后,打开miscrosoft visual sourcesafe,---create connection da ...
- vmware10安装win8x64(亲测)
首先,创建虚拟机,选择典型 下一步后 弄完后选择“完成”,这下就可以安装了 之后分区,之后选cdrom做启动,之后安装win8到第一分区
- Android 自定义View可拖动移动位置及边缘拉伸放大缩小
一.首先说一下定义这样一个View有什么用?在一些app中,需要设置头像,而用户选择的图片可能是使用摄像头拍摄,也可能是选择的相册里面的图片,总之,这样的图片大小不一,就比如在使用某个聊天软件的时候, ...
- cuda(1) 最大并发量
Created on 2013-8-5URL : http://blog.sina.com.cn/s/blog_a502f1a30101mi6t.html@author: zhxfl转载请说明出处 c ...