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

2023-11-02 20:30

本文主要是介绍Android应用升级构想和要点总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

原文链接:http://blog.csdn.net/richway2010/article/details/6408258

下面就如何对Android 手机上的应用进行升级相关知识的讲解:

一、原理及要点概述:

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

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

二、接下来具体介绍一下升级步骤:

  1. 签名步骤流程讲一下,很多初学者不是很清楚或容易犯错:

签名的作用: 是为了保证每个应用程序开发商合法ID,防止部分开放商可能通过使用相同的Package Name来混淆替换已经安装的程序,我们需要对我们发布的APK文件进行唯一签名,保证我们每次发布的版本的一致性(如自动更新不会因为版本不一致而无法安装)。

首先eclipse如图所示打开:

clip_image002

然后设置APK名称,一般默认项目名称:

clip_image004

点击下一步,新建一个keys,输入密钥,:

clip_image006

注:这个密钥很重要,每次升级都需要使用到。忘记密钥也只能是重新生成。从而会引发后面升级的一系列问题。

下一步如图,设置别名,组织机构等等基本信息,根据具体情况:

clip_image008

下一步如图选择保存的APK目录:

clip_image010

至此apk和密钥文件生成完毕:

clip_image012

接下来,当程序需要改变,我们如何打包升级APK文件:

第一步和上面一样,从第二步开始讲解:

注:一定要使用现有的密钥,输入之前设置的密码,不然安装APK将不被覆盖。

clip_image014

下一步如图,输入密码:

clip_image016

然后下一步,Finish掉。

至此APK升级文件创建好。

三、服务器端和客户端设计

  1. 服务器端设计:

设计方法应该有很多,下面介绍我的一种方法:

  • a.首先在服务器项目下建立一个文件夹来存放APK安装文件:
  • b.其次在src下建立一个资源文件,apkVersion.properties,属性定义如下:
[plain] view plain copy print ?
  1. apkVersion=1 存版本号apkSize=550kb 大小apkPath=http://xx8080/srv/apk/Demo.apk 升级文件 
apkVersion=1 存版本号apkSize=550kb 大小apkPath=http://xx8080/srv/apk/Demo.apk 升级文件
  • c.定义一个servlet来获取资源中的信息:

定义类:UpdateApkServlet.java

[java] view plain copy print ?
  1. //获取资源文件信息 
  2. static
  3. Properties ppt = new Properties(); 
  4. try {      
  5.     ppt.load(UpdateApkServlet.class .getResourceAsStream("/apkVersion.properties"));      
  6.     apkVersion = ppt.getProperty("apkVersion");      
  7.     apkSize = ppt.getProperty("apkSize");      
  8.     apkPath = ppt.getProperty("apkPath");  
  9. }catch (Exception e) {      
  10.     e.printStackTrace(); 
  11. }  
//获取资源文件信息
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] view plain copy print ?
  1. /**
  2. * 得到本地应用的版本信息 
  3. * @return
  4. */ 
  5. private int getAPKVersion(){ 
  6.     //APK版本判断 
  7.     int sdcardVersion = 0
  8.     String apkFilePath="sdcard/demo.apk"//安装包路径 
  9.     PackageManager pm = getPackageManager();         
  10.     PackageInfo info = pm.getPackageArchiveInfo(apkFilePath, PackageManager.GET_ACTIVITIES);          
  11. if(info != null){              
  12.     sdcardVersion=info.versionCode;       //得到版本信息              
  13.     Log.v(TAG, "Version="+sdcardVersion);         
  14. }          
  15. return sdcardVersion; 
/** * 得到本地应用的版本信息  * @return 
*/
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] view plain copy print ?
  1. Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
  2. 访问网络权限: uses-permission android:name="android.permission.INTERNET"  
Sdcard访问权限: uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
访问网络权限: uses-permission android:name="android.permission.INTERNET" 
  • 5、 更新安装 当用户点击应用时执行检查更新。相关代码参考:

//弹出框提示

[java] view plain copy print ?
  1. public Handler handler = new Handler() { 
  2. public void handleMessage(Message msg) { 
  3. super.handleMessage(msg);        
  4. Dialog dialog = new AlertDialog.Builder(MainActivity.this).setTitle("系统更新").setMessage("发现新版本,请更新!")  
  5. // 设置内容.setPositiveButton("确定",// 设置确定按钮new DialogInterface.OnClickListener() { 
  6. @Override 
  7. public void onClick(DialogInterface dialog, int which) { 
  8. pBar = new ProgressDialog(MainActivity.this); 
  9. pBar.setTitle("正在下载");pBar.setMessage("请稍候..."); 
  10. pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);downFile(apkPath);}}).setNegativeButton("取消", new DialogInterface.OnClickListener() { 
  11. public void onClick(DialogInterface dialog, int whichButton) { 
  12. // 点击"取消"按钮操作}}).create();// 创建 
  13. // 显示对话框 
  14. dialog.show(); 
  15.         } 
  16. }; 
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] view plain copy print ?
  1. /**
  2. * DOWNLOAD APK FILE BY URL
  3. * @param url
  4. */ 
  5. public void downFile(final String url) { 
  6. pBar.show(); 
  7. new Thread() { 
  8. public void run() { 
  9. HttpClient client = new DefaultHttpClient(); 
  10. // params[0]代表连接的 
  11. urlHttpGet get = new HttpGet(url); 
  12. HttpResponse response; 
  13. try
  14. response = client.execute(get); 
  15. HttpEntity entity = response.getEntity(); 
  16. long length = entity.getContentLength(); 
  17. InputStream is = entity.getContent(); 
  18. FileOutputStream fileOutputStream = null
  19. if (is != null) { 
  20. File file = new File(Environment.getExternalStorageDirectory(),"demo.apk"); 
  21. fileOutputStream = new FileOutputStream(file); 
  22. byte[] buf = new byte[1024]; 
  23. int ch = -1
  24. int count = 0
  25. while ((ch = is.read(buf)) != -1) { 
  26. // baos.write(buf, 0, ch); 
  27. fileOutputStream.write(buf, 0, ch); 
  28. count += ch;if (length > 0) {} 
  29. fileOutputStream.flush(); 
  30. if (fileOutputStream != null) { 
  31. fileOutputStream.close(); 
  32. down(); 
  33. } catch (ClientProtocolException e) { 
  34. e.printStackTrace(); 
  35. } catch (IOException e) { 
  36. e.printStackTrace(); 
  37. }.start(); 
  38. public void down() { 
  39. handler.post(new Runnable()  
  40. public void run() { 
  41. pBar.cancel(); 
  42. update(); 
  43. }}); 
/** * DOWNLOAD APK FILE BY URL * @param url 
*/
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] view plain copy print ?
  1. public void update() { 
  2. Intent intent = new Intent(Intent.ACTION_VIEW); 
  3. intent.setDataAndType(Uri.fromFile(new File("/sdcard/demo.apk")),"application/vnd.android.package-archive"); 
  4. startActivity(intent); 
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);
}


结束,供参考。

2011-5

 

这篇关于Android应用升级构想和要点总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/333118

相关文章

分布式锁在Spring Boot应用中的实现过程

《分布式锁在SpringBoot应用中的实现过程》文章介绍在SpringBoot中通过自定义Lock注解、LockAspect切面和RedisLockUtils工具类实现分布式锁,确保多实例并发操作... 目录Lock注解LockASPect切面RedisLockUtils工具类总结在现代微服务架构中,分布

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用IDEA部署Docker应用指南分享

《使用IDEA部署Docker应用指南分享》本文介绍了使用IDEA部署Docker应用的四步流程:创建Dockerfile、配置IDEADocker连接、设置运行调试环境、构建运行镜像,并强调需准备本... 目录一、创建 dockerfile 配置文件二、配置 IDEA 的 Docker 连接三、配置 Do

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

深入浅出SpringBoot WebSocket构建实时应用全面指南

《深入浅出SpringBootWebSocket构建实时应用全面指南》WebSocket是一种在单个TCP连接上进行全双工通信的协议,这篇文章主要为大家详细介绍了SpringBoot如何集成WebS... 目录前言为什么需要 WebSocketWebSocket 是什么Spring Boot 如何简化 We

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python中列表应用和扩展性实用详解

《python中列表应用和扩展性实用详解》文章介绍了Python列表的核心特性:有序数据集合,用[]定义,元素类型可不同,支持迭代、循环、切片,可执行增删改查、排序、推导式及嵌套操作,是常用的数据处理... 目录1、列表定义2、格式3、列表是可迭代对象4、列表的常见操作总结1、列表定义是处理一组有序项目的

C#中的Converter的具体应用

《C#中的Converter的具体应用》C#中的Converter提供了一种灵活的类型转换机制,本文详细介绍了Converter的基本概念、使用场景,具有一定的参考价值,感兴趣的可以了解一下... 目录Converter的基本概念1. Converter委托2. 使用场景布尔型转换示例示例1:简单的字符串到

Spring Boot Actuator应用监控与管理的详细步骤

《SpringBootActuator应用监控与管理的详细步骤》SpringBootActuator是SpringBoot的监控工具,提供健康检查、性能指标、日志管理等核心功能,支持自定义和扩展端... 目录一、 Spring Boot Actuator 概述二、 集成 Spring Boot Actuat

PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例

《PyTorch中的词嵌入层(nn.Embedding)详解与实战应用示例》词嵌入解决NLP维度灾难,捕捉语义关系,PyTorch的nn.Embedding模块提供灵活实现,支持参数配置、预训练及变长... 目录一、词嵌入(Word Embedding)简介为什么需要词嵌入?二、PyTorch中的nn.Em