`
825288003
  • 浏览: 110866 次
  • 性别: Icon_minigender_2
  • 来自: 北京
社区版块
存档分类
最新评论

android如何升级

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

一、原理及要点概述:

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









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

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

首先eclipse如图所示打开:



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



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



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

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



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



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



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

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

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



下一步如图,输入密码:



然后下一步,Finish掉。

至此APK升级文件创建好。

三、服务器端和客户端设计
1.服务器端设计:
设计方法应该有很多,下面介绍我的一种方法:

•a.首先在服务器项目下建立一个文件夹来存放APK安装文件:
•b.其次在src下建立一个资源文件,apkVersion.properties,属性定义如下:
[plain] view plaincopyprint?
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 plaincopyprint?
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.} 
12.}  
//获取资源文件信息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 plaincopyprint?
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;  16.} 
/** * 得到本地应用的版本信息 * @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 plaincopyprint?
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 plaincopyprint?
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() {@Overridepublic 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 plaincopyprint?
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.} 
30.} 
31.fileOutputStream.flush(); 
32.if (fileOutputStream != null) {  33.fileOutputStream.close(); 
34.} 
35.down(); 
36.} catch (ClientProtocolException e) {  37.e.printStackTrace(); 
38.} catch (IOException e) {  39.e.printStackTrace(); 
40.} 
41.} 
42.}.start(); 
43.} 
44.public void down() {  45.handler.post(new Runnable()   46.{ 
47.public void run() {  48.pBar.cancel(); 
49.update(); 
50.}}); 
51.} 
/** * 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 plaincopyprint?
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); 
5.} 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics