在线升级Android程序

http://www.javaeye.com/topic/792774 

如果某个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.     }  

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mDB = new MapVersionTable(this);
        
        if (checkNewVersion()) {
        	
        	if (apkUrl == null) 
        		return;
        	
        	downloadAPK(apkUrl);
        	killProcess();
        	installAPK();
        	finish();
        } else {
        	if (checkApp()) {
        		invokeAPK();
                finish();
        	}else {
        		downloadAPK(apkUrl);
        		installAPK();
            	finish();
        	}
        }
    }


其中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.     }  

private boolean checkNewVersion() {
		try {
			URL url=new URL(AppConfig.REST_URL);
			
			SAXParserFactory factory=SAXParserFactory.newInstance();
			factory.setNamespaceAware(true);
			factory.setValidating(false);
		    SAXParser parser=factory.newSAXParser();
		    InputStream is = url.openStream();
		    parser.parse(is, new DefaultHandler(){
				private String cur="";
				private int step;
				
				@Override
				public void startDocument() throws SAXException {
					step = 0;
				}
				
				@Override
				public void startElement(String uri, String localName,
						String qName, Attributes attributes)
						throws SAXException {
					cur = localName;
				}
				
				@Override
				public void characters(char[] ch, int start, int length)
						throws SAXException {
					String str = new String(ch, start, length).trim();
					if (str == null || str.equals(""))
						return;
					if (cur.equals("url")) {
						apkUrl = str;
					}
					if (cur.equals("map_version")) {
						mapVersion = str;
					}
				}
				
				@Override
				public void endElement(String uri, String localName,
						String qName) throws SAXException {
					step = step + 1;
				}
				
				@Override
				public void endDocument() throws SAXException {
					super.endDocument();
				}
			});
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (diffVersion(mapVersion)) 
			return true;
		else
			return false;
	}



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.     }  

private boolean diffVersion(String mapVersion) {
		String lastVersion = mDB.getLastMapVersion();
		if (lastVersion == null) {
			mDB.setMapVersion(mapVersion);
			return true;
		}
		
		if (!lastVersion.equals(mapVersion)) {
			mDB.setMapVersion(mapVersion);
			return true;
		}
		else
			return false;
	}



    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.     }  

private boolean checkApp() {
		Intent intent = new Intent(Intent.ACTION_VIEW);  
		intent.setClassName("com.android.settings", 
		        "com.android.settings.InstalledAppDetails"); 
		intent.putExtra("com.android.settings.ApplicationPkgName",  
				AppConfig.APKNAME);  
		List<ResolveInfo> acts = getPackageManager().queryIntentActivities(  
		        intent, 0);  
		if (acts.size() > 0) {  
			return true;
		} else
			return false;
	}




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.     }  

private void killProcess() {
		activityMan = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
		process = activityMan.getRunningAppProcesses();
		
		int len = process.size();
		for(int i = 0;i<len;i++) {
			if (process.get(i).processName.equals(AppConfig.PKG)) {
				android.os.Process.killProcess(process.get(i).pid);
			}
		}
	}



    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.     }  

private void installAPK() {
		String fileName = getSDPath() +"/download/"+AppConfig.APKNAME;
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(new File(fileName)), "application/vnd.android.package-archive");
		startActivity(intent);
	}



invokeAPK()根据包名,调用本地的应用。
 

Java代码 复制代码
  1. private void invokeAPK() {   
  2.         Intent i=new Intent();   
  3.         i.setComponent(new ComponentName(AppConfig.PKG, AppConfig.CLS));   
  4.         startActivity(i);   
  5.     }  

private void invokeAPK() {
		Intent i=new Intent();
		i.setComponent(new ComponentName(AppConfig.PKG, AppConfig.CLS));
		startActivity(i);
	}



最后,不要忘记关闭数据库 呵呵
 

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
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android 在线升级 Demo 是一个展示 Android 应用程序在线升级的示例程序在线升级是指在用户设备上通过网络下载并安装应用程序的更新版本。下面是一个简单的 Demo 的实现步骤: 1. 创建一个基础的 Android 应用程序,并确定其中需要进行在线升级的模块。 2. 在应用程序中添加在线升级的功能模块。这个功能模块应该包括能够检查服务器上是否存在新的应用程序版本的功能,以及能够下载和安装应用程序更新的功能。 3. 在服务器上搭建一个存放应用程序更新文件的下载中心。在服务器端,需要提供一个接口供应用程序向服务器请求最新的应用程序版本信息,并提供应用程序下载链接的功能。 4. 在应用程序中实现向服务器请求最新应用程序版本信息和下载链接的功能。可以使用 HTTP 或者其他通信库来发送请求并解析服务器返回的数据。 5. 在应用程序中实现下载和安装应用程序更新的功能。可以使用 Android 系统提供的下载管理器来下载应用程序更新文件,并通过隐式 Intent 打开应用程序更新文件进行安装。 6. 安装完成后,需要提示用户重启应用程序,使得新版本生效。 7. 在应用程序中保留一个版本号的记录,每次检查新版本时与服务器端返回的版本号进行对比,如果有新版本则进行下载和安装。 通过上述步骤,我们可以实现一个基本的 Android 在线升级 Demo。用户只需要打开应用程序,并检查是否有新版本可用,然后通过点击下载、安装等操作,即可完成应用程序在线升级
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值