Android自动检测版本及自动升级.docx
- 文档编号:6080292
- 上传时间:2023-01-03
- 格式:DOCX
- 页数:12
- 大小:111.87KB
Android自动检测版本及自动升级.docx
《Android自动检测版本及自动升级.docx》由会员分享,可在线阅读,更多相关《Android自动检测版本及自动升级.docx(12页珍藏版)》请在冰豆网上搜索。
Android自动检测版本及自动升级
步骤:
1.检测当前版本的信息AndroidManifest.xml-->manifest-->android:
versionName。
2.从服务器获取版本号(版本号存在于xml文件中)并与当前检测到的版本进行匹配,如果不匹配,提示用户进行升级,如果匹配则进入程序主界面。
3.当提示用户进行版本升级时,如果用户点击了确定,系统将自动从服务器上下载并进行自动升级,如果点击取消将进入程序主界面。
效果图:
获取当前程序的版本号:
[java]viewplaincopy
1/*
2*获取当前程序的版本号
3*/
4privateStringgetVersionName()throwsException{
5//获取packagemanager的实例
6PackageManagerpackageManager=getPackageManager();
7//getPackageName()是你当前类的包名,0代表是获取版本信息
8PackageInfopackInfo=packageManager.getPackageInfo(getPackageName(),0);
9returnpackInfo.versionName;
10}
获取服务器端的版本号:
[java]viewplaincopy
11/*
12*用pull解析器解析服务器返回的xml文件(xml封装了版本号)
13*/
14publicstaticUpdataInfogetUpdataInfo(InputStreamis)throwsException{
15XmlPullParserparser=Xml.newPullParser();
16parser.setInput(is,"utf-8");//设置解析的数据源
17inttype=parser.getEventType();
18UpdataInfoinfo=newUpdataInfo();//实体
19while(type!
=XmlPullParser.END_DOCUMENT){
20switch(type){
21caseXmlPullParser.START_TAG:
22if("version".equals(parser.getName())){
23info.setVersion(parser.nextText());//获取版本号
24}elseif("url".equals(parser.getName())){
25info.setUrl(parser.nextText());//获取要升级的APK文件
26}elseif("description".equals(parser.getName())){
27info.setDescription(parser.nextText());//获取该文件的信息
28}
29break;
30}
31type=parser.next();
32}
33returninfo;
34}
从服务器下载apk:
[java]viewplaincopy
35publicstaticFilegetFileFromServer(Stringpath,ProgressDialogpd)throwsException{
36//如果相等的话表示当前的sdcard挂载在手机上并且是可用的
37if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
38URLurl=newURL(path);
39HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
40conn.setConnectTimeout(5000);
41//获取到文件的大小
42pd.setMax(conn.getContentLength());
43InputStreamis=conn.getInputStream();
44Filefile=newFile(Environment.getExternalStorageDirectory(),"updata.apk");
45FileOutputStreamfos=newFileOutputStream(file);
46BufferedInputStreambis=newBufferedInputStream(is);
47byte[]buffer=newbyte[1024];
48intlen;
49inttotal=0;
50while((len=bis.read(buffer))!
=-1){
51fos.write(buffer,0,len);
52total+=len;
53//获取当前下载量
54pd.setProgress(total);
55}
56fos.close();
57bis.close();
58is.close();
59returnfile;
60}
61else{
62returnnull;
63}
64}
匹配、下载、自动安装:
[java]viewplaincopy
65/*
66*从服务器获取xml解析并进行比对版本号
67*/
68publicclassCheckVersionTaskimplementsRunnable{
69
70publicvoidrun(){
71try{
72//从资源文件获取服务器地址
73Stringpath=getResources().getString(R.string.serverurl);
74//包装成url的对象
75URLurl=newURL(path);
76HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
77conn.setConnectTimeout(5000);
78InputStreamis=conn.getInputStream();
79info=UpdataInfoParser.getUpdataInfo(is);
80
81if(info.getVersion().equals(versionname)){
82Log.i(TAG,"版本号相同无需升级");
83LoginMain();
84}else{
85Log.i(TAG,"版本号不同,提示用户升级");
86Messagemsg=newMessage();
87msg.what=UPDATA_CLIENT;
88handler.sendMessage(msg);
89}
90}catch(Exceptione){
91//待处理
92Messagemsg=newMessage();
93msg.what=GET_UNDATAINFO_ERROR;
94handler.sendMessage(msg);
95e.printStackTrace();
96}
97}
98}
99
100Handlerhandler=newHandler(){
101
102@Override
103publicvoidhandleMessage(Messagemsg){
104//TODOAuto-generatedmethodstub
105super.handleMessage(msg);
106switch(msg.what){
107caseUPDATA_CLIENT:
108//对话框通知用户升级程序
109showUpdataDialog();
110break;
111caseGET_UNDATAINFO_ERROR:
112//服务器超时
113Toast.makeText(getApplicationContext(),"获取服务器更新信息失败",1).show();
114LoginMain();
115break;
116caseDOWN_ERROR:
117//下载apk失败
118Toast.makeText(getApplicationContext(),"下载新版本失败",1).show();
119LoginMain();
120break;
121}
122}
123};
124
125/*
126*
127*弹出对话框通知用户更新程序
128*
129*弹出对话框的步骤:
130*1.创建alertDialog的builder.
131*2.要给builder设置属性,对话框的内容,样式,按钮
132*3.通过builder创建一个对话框
133*4.对话框show()出来
134*/
135protectedvoidshowUpdataDialog(){
136AlertDialog.Builderbuiler=newBuilder(this);
137builer.setTitle("版本升级");
138builer.setMessage(info.getDescription());
139//当点确定按钮时从服务器上下载新的apk然后安装
140builer.setPositiveButton("确定",newOnClickListener(){
141publicvoidonClick(DialogInterfacedialog,intwhich){
142Log.i(TAG,"下载apk,更新");
143downLoadApk();
144}
145});
146//当点取消按钮时进行登录
147builer.setNegativeButton("取消",newOnClickListener(){
148publicvoidonClick(DialogInterfacedialog,intwhich){
149//TODOAuto-generatedmethodstub
150LoginMain();
151}
152});
153AlertDialogdialog=builer.create();
154dialog.show();
155}
156
157/*
158*从服务器中下载APK
159*/
160protectedvoiddownLoadApk(){
161finalProgressDialogpd;//进度条对话框
162pd=newProgressDialog(this);
163pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
164pd.setMessage("正在下载更新");
165pd.show();
166newThread(){
167@Override
168publicvoidrun(){
169try{
170Filefile=DownLoadManager.getFileFromServer(info.getUrl(),pd);
171sleep(3000);
172installApk(file);
173pd.dismiss();//结束掉进度条对话框
174}catch(Exceptione){
175Messagemsg=newMessage();
176msg.what=DOWN_ERROR;
177handler.sendMessage(msg);
178e.printStackTrace();
179}
180}}.start();
181}
182
183//安装apk
184protectedvoidinstallApk(Filefile){
185Intentintent=newIntent();
186//执行动作
187intent.setAction(Intent.ACTION_VIEW);
188//执行的数据类型
189intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
190startActivity(intent);
191}
192
193/*
194*进入程序的主界面
195*/
196privatevoidLoginMain(){
197Intentintent=newIntent(this,MainActivity.class);
198startActivity(intent);
199//结束掉当前的activity
200this.finish();
201}
UpdataInfo:
[java]viewplaincopy
202publicclassUpdataInfo{
203privateStringversion;
204privateStringurl;
205privateStringdescription;
206publicStringgetVersion(){
207returnversion;
208}
209publicvoidsetVersion(Stringversion){
210this.version=version;
211}
212publicStringgetUrl(){
213returnurl;
214}
215publicvoidsetUrl(Stringurl){
216this.url=url;
217}
218publicStringgetDescription(){
219returndescription;
220}
221publicvoidsetDescription(Stringdescription){
222this.description=description;
223}
224}
update.xml:
[html]viewplaincopy
225
xmlversion="1.0"encoding="utf-8"?
>
226
227
228
//192.168.1.187:
8080/mobilesafe.apk
229
230
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Android 自动检测 版本 自动 升级