`

Android开发中APK升级的两种思路

阅读更多

 

       项目的第一版已经成型,要结束了,还差为了后期准备升级的功能没有实现,也就是apk升级,这种需求非常常见,今天上网查了一些资料,听项目经理给我介绍了两种常见的apk升级的思路,总结一下:

 

一、在服务器端放置一个升级的配置文件,xml或者是json的文件,这个配置文件一般要包括目前服务器上apk的版本信息、下载新版apk的URL地址以及对这个apk升级的描述,一个例子:

 

<?xml version="1.0" encoding="utf-8"?>
<info>
	<version>1.0</version>
	<description>1.0版本,速速下载</description>
	<apkurl>http://127.0.0.1:8080/new.apk</apkurl>
</info>

       

       既然服务器上有了描述版本升级的配置文件之后,那么我们就可以在app中解析这个配置文件,判断目前使用的APP版本信息和服务器上的版本信息是否一致。服务器上的apk版本信息通过解析上面的配置文件获得,目前手机上的APP的版本信息通过以下代码获得:

 

try {
	int ersionCode = getApplication().getPackageManager().getPackageInfo(getPackageName(), 0).versionCode;
} catch (NameNotFoundException e) {
	e.printStackTrace();
}

 

 之后,如果发现两个版本信息不同,就需要提醒用户升级了。

 

二、第二种思路比较简单,其实跟第一种是一样的,就是让服务器端提供一个版本升级的接口,我们Android客户端将apk的版本发送到服务器接口,然后这个接口返回是否要进行升级的信息,如果要升级,那么就提醒用户升级,否则就是不升级。因为这个方法简单,我就用这个方式了。。。

 

三、在做版本升级这个需求时用到的核心代码:

1、如果是配置文件的方式的话,需要解析服务器的xml或json文件,代码之前的文章写过,不重复了。

2、还有就是获取当前APP版本信息代码,上面给出了。

3、从服务器下载apk文件的工具类:

package util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.ProgressDialog;

/**
 * 文件下载工具类
 * 
 * @author HJW
 *
 * @date 2015年12月23日
 */
public class DownLoadUtil {
	
	/**
	 * 下载文件的操作
	 * @param serverPath 服务器文件路径
	 * @param savedPath 本地保存路径
	 * @param pd 进度条对话框
	 * @return 下载成功,返回文件对象; 下载失败,返回null
	 */
	public static File download( String serverPath, String savedPath, ProgressDialog pd ){
		
		try {
			URL url = new URL(serverPath);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setConnectTimeout(5000);
			conn.setRequestMethod("GET");
			int code = conn.getResponseCode();
			if ( code == 200 ) {
				int fileSize = conn.getContentLength()/1024;
				pd.setMax(fileSize);
				int total = 0;
				InputStream is = conn.getInputStream();
				File file = new File(savedPath);
				FileOutputStream fos = new FileOutputStream(file);
				byte[] buffer = new byte[1024];
				int len = 0;
				while ( (len = is.read(buffer)) != -1 ) {
					fos.write(buffer, 0, len);
					total += (len/1024);
					pd.setProgress(total);
				}
				fos.flush();
				fos.close();
				is.close();
				return file;
			} else {
				return null;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	} 
	
	/**
	 * 获取服务器文件的名称
	 * @param serverPath 服务器文件路径
	 * @return 服务器文件的名称
	 */
	public static String getFileName( String serverPath ){
		return serverPath.substring(serverPath.lastIndexOf("/")+1);
	}
	
}

 4、安装apk的代码:

	/**
	 * 安装apk文件
	 * 
	 * @param file
	 *            要安装的apk文件
	 */
	private void installApk(File file) {

		Intent intent = new Intent();
		intent.setAction("android.intent.action.VIEW");
		intent.addCategory("android.intent.category.DEFAULT");
		intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
		startActivity(intent);
	}

 

2
3
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics