在线升级Android应用程序的思路

如果某个app有内嵌的sqlite数据库,则可以在应用程序app前增加一个专门用于升级的应用update app。在升级时先使用update app,如果有新版本的话可以去服务端下载最新的app,如果没有新版本的话则直接调用本地的app。 

Update app的大致思路是这样的: 
  
Java代码   收藏代码
  1. public void onCreate(Bundle savedInstanceState) {  
  2.         super.onCreate(savedInstanceState);  
  3.         setContentView(R.layout.main);  
  4.   
  5.         mDB = new MapVersionTable(this);  
  6.           
  7.         if (checkNewVersion()) {  
  8.               
  9.             if (apkUrl == null)   
  10.                 return;  
  11.               
  12.             downloadAPK(apkUrl);  
  13.             killProcess();  
  14.             installAPK();  
  15.             finish();  
  16.         } else {  
  17.             if (checkApp()) {  
  18.                 invokeAPK();  
  19.                 finish();  
  20.             }else {  
  21.                 downloadAPK(apkUrl);  
  22.                 installAPK();  
  23.                 finish();  
  24.             }  
  25.         }  
  26.     }  

其中MapVersionTable是用于update app记录应用程序版本的。 

checkNewVersion()用于检查是否有新版本的存在,并将服务端的版本号存入mapVersion变量,将服务端的应用地址存放在apkUrl变量。这段检查应用程序的方法,其实是利用rest方式进行访问,当然也可以用web service或者其他通讯方式。 
Java代码   收藏代码
  1. private boolean checkNewVersion() {  
  2.         try {  
  3.             URL url=new URL(AppConfig.REST_URL);  
  4.               
  5.             SAXParserFactory factory=SAXParserFactory.newInstance();  
  6.             factory.setNamespaceAware(true);  
  7.             factory.setValidating(false);  
  8.             SAXParser parser=factory.newSAXParser();  
  9.             InputStream is = url.openStream();  
  10.             parser.parse(is, new DefaultHandler(){  
  11.                 private String cur="";  
  12.                 private int step;  
  13.                   
  14.                 @Override  
  15.                 public void startDocument() throws SAXException {  
  16.                     step = 0;  
  17.                 }  
  18.                   
  19.                 @Override  
  20.                 public void startElement(String uri, String localName,  
  21.                         String qName, Attributes attributes)  
  22.                         throws SAXException {  
  23.                     cur = localName;  
  24.                 }  
  25.                   
  26.                 @Override  
  27.                 public void characters(char[] ch, int start, int length)  
  28.                         throws SAXException {  
  29.                     String str = new String(ch, start, length).trim();  
  30.                     if (str == null || str.equals(""))  
  31.                         return;  
  32.                     if (cur.equals("url")) {  
  33.                         apkUrl = str;  
  34.                     }  
  35.                     if (cur.equals("map_version")) {  
  36.                         mapVersion = str;  
  37.                     }  
  38.                 }  
  39.                   
  40.                 @Override  
  41.                 public void endElement(String uri, String localName,  
  42.                         String qName) throws SAXException {  
  43.                     step = step + 1;  
  44.                 }  
  45.                   
  46.                 @Override  
  47.                 public void endDocument() throws SAXException {  
  48.                     super.endDocument();  
  49.                 }  
  50.             });  
  51.         } catch (MalformedURLException e) {  
  52.             e.printStackTrace();  
  53.         } catch (ParserConfigurationException e) {  
  54.             e.printStackTrace();  
  55.         } catch (SAXException e) {  
  56.             e.printStackTrace();  
  57.         } catch (IOException e) {  
  58.             e.printStackTrace();  
  59.         }  
  60.         if (diffVersion(mapVersion))   
  61.             return true;  
  62.         else  
  63.             return false;  
  64.     }  


diffVersion()是将服务端版本号和本地版本号进行比较,如果存在新版本,则将最新的版本号存入数据库并调用downloadAPK();如果不存在新版本,则会调用本地的app。不过在调用本地app前需要先判断本地的app是否安装,这个时候使用checkApp()方法。 
Java代码   收藏代码
  1. private boolean diffVersion(String mapVersion) {  
  2.         String lastVersion = mDB.getLastMapVersion();  
  3.         if (lastVersion == null) {  
  4.             mDB.setMapVersion(mapVersion);  
  5.             return true;  
  6.         }  
  7.           
  8.         if (!lastVersion.equals(mapVersion)) {  
  9.             mDB.setMapVersion(mapVersion);  
  10.             return true;  
  11.         }  
  12.         else  
  13.             return false;  
  14.     }  


    checkApp()该方法用于检查本地是否安装该app 
Java代码   收藏代码
  1. private boolean checkApp() {  
  2.         Intent intent = new Intent(Intent.ACTION_VIEW);    
  3.         intent.setClassName("com.android.settings",   
  4.                 "com.android.settings.InstalledAppDetails");   
  5.         intent.putExtra("com.android.settings.ApplicationPkgName",    
  6.                 AppConfig.APKNAME);    
  7.         List<ResolveInfo> acts = getPackageManager().queryIntentActivities(    
  8.                 intent, 0);    
  9.         if (acts.size() > 0) {    
  10.             return true;  
  11.         } else  
  12.             return false;  
  13.     }  



killProcess()是杀掉进程,防止升级时该应用还在使用。 
Java代码   收藏代码
  1. private void killProcess() {  
  2.         activityMan = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);  
  3.         process = activityMan.getRunningAppProcesses();  
  4.           
  5.         int len = process.size();  
  6.         for(int i = 0;i<len;i++) {  
  7.             if (process.get(i).processName.equals(AppConfig.PKG)) {  
  8.                 android.os.Process.killProcess(process.get(i).pid);  
  9.             }  
  10.         }  
  11.     }  


    installAPK()将下载的app进行安装 
Java代码   收藏代码
  1. private void installAPK() {  
  2.         String fileName = getSDPath() +"/download/"+AppConfig.APKNAME;  
  3.         Intent intent = new Intent(Intent.ACTION_VIEW);  
  4.         intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");  
  5.         startActivity(intent);  
  6.     }  


invokeAPK()根据包名,调用本地的应用。 
Java代码   收藏代码
  1. private void invokeAPK() {  
  2.         Intent i=new Intent();  
  3.         i.setComponent(new ComponentName(AppConfig.PKG, AppConfig.CLS));  
  4.         startActivity(i);  
  5.     }  


最后,不要忘记关闭数据库 呵呵
Java代码   收藏代码
  1. protected void onDestroy() {  
  2.         super.onDestroy();  
  3.         try {             
  4.             mDB.close(); // be sure to close  
  5.         } catch (Exception e) {  
  6.         }  
  7.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值