您的位置:首页 > 移动开发 > Android开发

Android应用升级构想和要点总结

2014-07-21 11:52 267 查看
一、原理及要点概述:

手机软件一般在运行时会把服务端的版本信息和当前手机中的版本进行比较。从而得知需不需要更新。Android上推介版本比较更新这种方式。
如果服务器端有新版本,我们需要先下载这个APK到我们的sdcard中,然后对其进行安装。
我们一定要保证每次安装的keystore密钥文件是相同的。这样Android手机才会提醒你替换新版本。
Android区分软件的不同是通过包名,身份的认证是通过签名。只有相同签名的APK才可以安装。不然安装就会失败。

注:如果你每次发布APK文件时都新建一个key文件会导致无法正确安装,我们必须先卸载老的版本才能执行新版本安装。这样你就必须要通知每个客户去把软件卸载后重新下载安装。

二、接下来具体介绍一下升级步骤
签名步骤流程讲一下,很多初学者不是很清楚或容易犯错:
签名的作用: 是为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序,我们需要对我们发布的APK文件进行唯一签名,保证我们每次发布的版本的一致性(如自动更新不会因为版本不一致而无法安装)。

三、服务器端和客户端设计
服务器端设计:
设计方法应该有很多,下面介绍我的一种方法:
a.首先在服务器项目下建立一个文件夹来存放APK安装文件:
b.其次在src下建立一个资源文件,apkVersion.properties,属性定义如下:
apkVersion=1 存版本号apkSize=550kb 大小apkPath=http://xx8080/srv/apk/Demo.apk 升级文件
c.定义一个servlet来获取资源中的信息:
定义类:UpdateApkServlet.java
//获取资源文件信息
static {
Properties ppt = new Properties();
try {
ppt.load(UpdateApkServlet.class .getResourceAsStream("/apkVersion.properties"));
apkVersion = ppt.getProperty("apkVersion");
apkSize = ppt.getProperty("apkSize");
apkPath = ppt.getProperty("apkPath");
}catch (Exception e) {
e.printStackTrace();
}
}

获取资源,然后生成JSON字串返回客户端处理。 注:当客户端版本有更新,服务器端只要把APK文件拷贝到APK目录,然后更新apkVersion.properties文件中的信息就可以了,切记。
客户端设计:
1、 客户端首先获取服务器的版本信息(http方式获取)。
2、 如何获取本地客户端的版本信息 如下参考代码:
[java]

private int getAPKVersion(){
//APK版本判断
int sdcardVersion = 0;
String apkFilePath="sdcard/demo.apk"; //安装包路径
PackageManager pm = getPackageManager();
PackageInfo info = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);
if(info != null){
sdcardVersion=info.versionCode; //得到版本信息
Log.v(TAG, "Version="+sdcardVersion);
}
return sdcardVersion;
}

3、 版本比较,如果版本相同,则不执行更新,不同才进行更新操作。 这里插入客户端版本设置介绍: 客户端版本设置在AndroidManifest.xml文件中,里面有两个属性可进行版本信息设置, android:versionCode="1" 版本号 android:versionName="1.1" 版本名称 这个版本号需要和服务器端对应。
4、 需要的权限设置
[plain]
Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
访问网络权限: uses-permission android:name="android.permission.INTERNET"
5、 更新安装 当用户点击应用时执行检查更新。相关代码参考:
//弹出框提示
[java]
public Handler handler = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系统更新").setMessage("发现新版本,请更新!")
// 设置内容.setPositiveButton("确定",// 设置确定按钮new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
pBar = new ProgressDialog(MainActivity.this);
pBar.setTitle("正在下载");pBar.setMessage("请稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);downFile(apkPath);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// 点击"取消"按钮操作}}).create();// 创建
// 显示对话框
dialog.show();
}
};

//下载
[java]

public void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
// params[0]代表连接的
urlHttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(Environment.getExternalStorageDirectory(),"demo.apk");
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
// baos.write(buf, 0, ch);
fileOutputStream.write(buf, 0, ch);
count += ch;if (length > 0) {}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public void down() {
handler.post(new Runnable()
{
public void run() {
pBar.cancel();
update();
}});
}

//更新升级
[java]
public void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File("/sdcard/demo.apk")),"application/vnd.android.package-archive");
startActivity(intent);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: