为什么80%的码农都做不了架构师?>>>   

步骤:

1.检测当前版本的信息AndroidManifest.xml-->manifest-->android:versionName。

2.从服务器获取版本号(版本号存在于xml文件中)并与当前检测到的版本进行匹配,如果不匹配,提示用户进行升级,如果匹配则进入程序主界面。

3.当提示用户进行版本升级时,如果用户点击了确定,系统将自动从服务器上下载并进行自动升级,如果点击取消将进入程序主界面。

?

1
2
3
4
5
6
7
8
9
10
11
12
获取当前程序的版本号:
1 . /* 
2. * 获取当前程序的版本号  
3. */  
4 . private String getVersionName()  throws Exception{  
5 .     //获取packagemanager的实例    
6 .    PackageManager packageManager = getPackageManager();  
7 .     //getPackageName()是你当前类的包名,0代表是获取版本信息   
8 .    PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(),  0 );  
9 .     return packInfo.versionName;   
10 .}

?

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
获取服务器端的版本号:
1 . /* 
2. * 用pull解析器解析服务器返回的xml文件 (xml封装了版本号) 
3. */  
4 . public static UpdataInfo getUpdataInfo(InputStream is)  throws Exception{  
5 .    XmlPullParser  parser = Xml.newPullParser();    
6 .    parser.setInput(is,  "utf-8" ); //设置解析的数据源    
7 .     int type = parser.getEventType();  
8 .    UpdataInfo info =  new UpdataInfo(); //实体   
9 .     while (type != XmlPullParser.END_DOCUMENT ){  
10 .         switch (type) {  
11 .         case XmlPullParser.START_TAG:  
12 .             if ( "version" .equals(parser.getName())){  
13 .                info.setVersion(parser.nextText());  //获取版本号   
14 .            } else if ( "url" .equals(parser.getName())){  
15 .                info.setUrl(parser.nextText());  //获取要升级的APK文件   
16 .            } else if ( "description" .equals(parser.getName())){  
17 .                info.setDescription(parser.nextText());  //获取该文件的信息   
18 .            }  
19 .             break ;  
20 .        }  
21 .        type = parser.next();  
22 .    }  
23 .     return info;  
24 .}

?

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
从服务器下载apk:
1 . public static File getFileFromServer(String path, ProgressDialog pd)  throws Exception{  
2 .     //如果相等的话表示当前的sdcard挂载在手机上并且是可用的   
3 .     if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){  
4 .        URL url =  new URL(path);  
5 .        HttpURLConnection conn =  (HttpURLConnection) url.openConnection();  
6 .        conn.setConnectTimeout( 5000 );  
7 .         //获取到文件的大小    
8 .        pd.setMax(conn.getContentLength());  
9 .        InputStream is = conn.getInputStream();  
10 .        File file =  new File(Environment.getExternalStorageDirectory(),  "updata.apk" );  
11 .        FileOutputStream fos =  new FileOutputStream(file);  
12 .        BufferedInputStream bis =  new BufferedInputStream(is);  
13 .         byte [] buffer =  new byte [ 1024 ];  
14 .         int len ;  
15 .         int total= 0 ;  
16 .         while ((len =bis.read(buffer))!=- 1 ){  
17 .            fos.write(buffer,  0 , len);  
18 .            total+= len;  
19 .             //获取当前下载量   
20 .            pd.setProgress(total);  
21 .        }  
22 .        fos.close();  
23 .        bis.close();  
24 .        is.close();  
25 .         return file;  
26 .    }  
27 .     else {  
28 .         return null ;  
29 .    }  
30 .}

匹配、下载、自动安装:

?

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
/*
  * 从服务器获取xml解析并进行比对版本号 
  */ 
public class CheckVersionTask  implements Runnable{ 
   
     public void run() { 
         try
             //从资源文件获取服务器 地址   
             String path = getResources().getString(R.string.serverurl); 
             //包装成url的对象   
             URL url =  new URL(path); 
             HttpURLConnection conn =  (HttpURLConnection) url.openConnection();  
             conn.setConnectTimeout( 5000 ); 
             InputStream is =conn.getInputStream();  
             info =  UpdataInfoParser.getUpdataInfo(is); 
               
             if (info.getVersion().equals(versionname)){ 
                 Log.i(TAG, "版本号相同无需升级" ); 
                 LoginMain(); 
             } else
                 Log.i(TAG, "版本号不同 ,提示用户升级 " ); 
                 Message msg =  new Message(); 
                 msg.what = UPDATA_CLIENT; 
                 handler.sendMessage(msg); 
            
         catch (Exception e) { 
             // 待处理   
             Message msg =  new Message(); 
             msg.what = GET_UNDATAINFO_ERROR; 
             handler.sendMessage(msg); 
             e.printStackTrace(); 
         }  
    
}  

?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Handler handler =  new Handler(){     
     @Override 
     public void handleMessage(Message msg) { 
         // TODO Auto-generated method stub  
         super .handleMessage(msg); 
         switch (msg.what) { 
         case UPDATA_CLIENT: 
             //对话框通知用户升级程序   
             showUpdataDialog(); 
             break
             case GET_UNDATAINFO_ERROR: 
                 //服务器超时   
                 Toast.makeText(getApplicationContext(),  "获取服务器更新信息失败" 1 ).show(); 
                 LoginMain(); 
             break ;   
             case DOWN_ERROR: 
                 //下载apk失败  
                 Toast.makeText(getApplicationContext(),  "下载新版本失败" 1 ).show(); 
                 LoginMain(); 
             break ;   
            
    
};

?

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
/*
 
  * 弹出对话框通知用户更新程序 
 
  * 弹出对话框的步骤:
  *  1.创建alertDialog的builder.  
  *  2.要给builder设置属性, 对话框的内容,样式,按钮
  *  3.通过builder 创建一个对话框
  *  4.对话框show()出来  
  */ 
protected void showUpdataDialog() { 
     AlertDialog.Builder builer =  new Builder( this ) ;  
     builer.setTitle( "版本升级" ); 
     builer.setMessage(info.getDescription()); 
     //当点确定按钮时从服务器上下载 新的apk 然后安装   
     builer.setPositiveButton( "确定" new OnClickListener() { 
     public void onClick(DialogInterface dialog,  int which) { 
             Log.i(TAG, "下载apk,更新" ); 
             downLoadApk(); 
         }    
     }); 
     //当点取消按钮时进行登录  
     builer.setNegativeButton( "取消" new OnClickListener() { 
         public void onClick(DialogInterface dialog,  int which) { 
             // TODO Auto-generated method stub  
             LoginMain(); 
        
     }); 
     AlertDialog dialog = builer.create(); 
     dialog.show(); 
}

?

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
/*
  * 从服务器中下载APK
  */ 
protected void downLoadApk() { 
     final ProgressDialog pd;     //进度条对话框  
     pd =  new  ProgressDialog( this ); 
     pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
     pd.setMessage( "正在下载更新" ); 
     pd.show(); 
     new Thread(){ 
         @Override 
         public void run() { 
             try
                 File file = DownLoadManager.getFileFromServer(info.getUrl(), pd); 
                 sleep( 3000 ); 
                 installApk(file); 
                 pd.dismiss();  //结束掉进度条对话框  
             catch (Exception e) { 
                 Message msg =  new Message(); 
                 msg.what = DOWN_ERROR; 
                 handler.sendMessage(msg); 
                 e.printStackTrace(); 
            
         }}.start(); 
}

?

1
2
3
4
5
6
7
8
9
//安装apk   
protected void installApk(File file) { 
     Intent intent =  new Intent(); 
     //执行动作  
     intent.setAction(Intent.ACTION_VIEW); 
     //执行的数据类型  
     intent.setDataAndType(Uri.fromFile(file),  "application/vnd.Android.package-archive" ); //编者按:此处Android应为android,否则造成安装不了   
     startActivity(intent); 
}

?

1
2
3
4
5
6
7
8
9
/*
  * 进入程序的主界面
  */ 
private void LoginMain(){ 
     Intent intent =  new Intent( this ,MainActivity. class ); 
     startActivity(intent); 
     //结束掉当前的activity   
     this .finish(); 
}

二、参考后使用情况:
1.可以下载apk,但安装失败:

1)以为配置文件中需定义了android.permission.INSTALL_PACKAGES,其实不需;

2)以为是要在执行安装的activity中配置

?

1
2
3
4
< intent-filter >
         < action android:name = "android.intent.action.VIEW" />
         < category android:name = "android.intent.category.DEFAULT" />
</ intent-filter >

,其实不是;

3)代码中application/vnd.Android.package-archive有一个字母A大写了,改小写后解决;

转载于:https://my.oschina.net/hjwsun/blog/620132

Android实现版本更新提示相关推荐

  1. thinksnsv4.6运行php,社交开源系统ThinkSNS V4.6.4更新,版本更新提示功能上线

    T4最新版本ThinkSNS V4.6.4已于9月12日中午发布,我们一直在坚持维护ThinkSNS V4,所以大家放心使用,本次主要新增版本更新提示功能,同时有十多个修复和优化内容. 第一个新增功能 ...

  2. Android实现版本更新和自动安装

    直接运行的项目和打包的项目apk签名不同,所以不能直接用开发工具运行项目进行版本更新.需要用apk打包安装的形式更新,否则会提示"签名冲突",无法完成覆盖安装 /** 版本更新 * ...

  3. 自定义Dialog提示框高仿QQ浏览器版本更新提示框

    前言: 今天是5月7号,真的好久没有写博客了,时光匆匆,我总感觉自己忙忙碌碌似的,静想片刻确实是挺忙的,但是在繁忙当中却缺少了总结归纳,相信大家都知道总结归纳的重要性了,今天我要和大家分享我的自定义D ...

  4. android studio 如何提示方法的用法

    方法/步骤1在 Eclipse中鼠标放上去就可以提示方法的用法,实际上Android Studio也可以设置的.如图 Preferences > Editor >Generan> S ...

  5. Xamarin Android设置界面提示类型错误

    Xamarin Android设置界面提示类型错误 错误信息:Integer types not allow (at 'padding' with value '10') Android界面属性的长度 ...

  6. Android实现退出提示的功能

    摘要:本文主要是实现在Android中退出提示的功能,平常使用Android手机时,点击返回键时,由于不小心点击返回键过快,导至程序返回试界面之后直接退出程序,导至用户还要再重新登录一次.为解决此类问 ...

  7. php cms 的模板修改,phpcms v9后台登陆模板修改方法和程序版本更新提示修改方法...

    Phpcms V9后台登陆及版本更新提示的自定义修改 一.Phpcms V9后台登陆模板修改方法 1. 找到登陆模板文件phpcms/modules/admin/emplates/login.tpl: ...

  8. [转]Android 代码自动提示功能

    源地址http://blog.sina.com.cn/s/blog_7dbac12501019mbh.html 或者http://blog.csdn.net/longvslove/article/de ...

  9. Android Studio编译提示如下attribute layout_constraintBottom_toBottomOf (aka com.luck.pictureselector:layou

    1 问题 Android Studio编译提示错误如下 AAPT: error: attribute layout_constraintBottom_toBottomOf (aka com.luck. ...

最新文章

  1. 《一个操作系统的实现》——pmtest1.asm详解
  2. Vue.js 从 Vue Router 0.7.x 迁移
  3. Codeforces Round #717 (Div. 2) D(倍增dp)
  4. java使用validator进行校验
  5. linux dhcp服务软包,dpkg包管理器详解
  6. 【微信小程序开发•系列文章七】websocket
  7. 调研AutoGluon数据处理与Tabular-NN
  8. 吴恩达深度学习笔记 最全最详细!这一篇足够了!
  9. Gap Statistic 间隔统计量
  10. html 显示 16进制 颜色,16进制颜色(html颜色值)
  11. 基于Python的高校网络课程数据分析
  12. python拼音四线格书写格式_Python 中拼音庫 PyPinyin 的用法
  13. 抓包常用工具使用简介
  14. 【英语语法入门】第44讲 假设(03)与过去事实相反的虚拟语气
  15. 2013年,移动互联网行业技术趋势前瞻
  16. 数据库小技能:序列和伪列
  17. java设计模式 之 模板方法模式
  18. android拍照保存到系统相册,调用系统相机拍照,并且保存到系统相册的一般套路...
  19. Postgresql进程卡住无法退出原因和解决方法
  20. 极狐公司官方澄清声明

热门文章

  1. Web Intents:Google的内部WebApp互联机制
  2. 忠告14:神原裕司郎:成功源于积累
  3. 如何在 Linux 中创建一个共享目录
  4. 渐变显示渐变消失的BackgroundView
  5. 解决Fiddler不能监听Java HttpURLConnection请求的方法
  6. Spring整合Hessian
  7. UPDATE 时主键冲突引发的思考
  8. [快速] 一行指令暫時隱藏 Mac 桌面檔案 – 讓你凌亂的桌面不會被看見 - TechMoon 科技月球...
  9. 《应用时间序列分析:R软件陪同》——1.5 习题
  10. NEC:借助AI撬动未来物联网世界