Android版本更新知识(检测、升级)总结
本文对Android版本更新的知识做全面的总结,主要包括开发中版本的设置,如何检测本程序的版本,版本的更新判断和显示,新版本程序的安装。
一.版本的基础知识
版本控制的属性包括versionCode和versionName。
(一)versionCode
版本号(versionCode)是相对比较重要的一个属性。versionCode是一个Integer类型的值。所以大家在设置的时候,不要将versionCode设置的太大,最好不要超过Integer的取值范围(当然一般也是不会超过的),一般大家在发布自己的第一个应用到市场的时候,版本取值为1(versionCode=1),这也是目前典型和普遍的做法。然后,每次发布更新版本时可以递增versionCode的值。
(二)versionName
版本名(versionName)一个值为String类型的属性,一般和VersionCode成对出现。VersionCode是方便程序开发者运行和维护Application而设置的一个有效的值。versionName是一个版本的描述,给用户看的,也是用户放在各个第3方平台上提供给使用者看的一个版本名,可以说是对VersionCode的解释和描述。一般格式可以为:1.1.2。(major.minor.point)的形式。
(三)版本控制小结
版本号(versionCode)是用于判断是否升级的,一般每次版本更新,版本号加一。如果获取服务器上的版本号比检测到本程序的版本号高,那么提示升级。
版本名(versionName)用于显示版本改变的幅度大小,比如从2.0.1改变为2.0.2可能只是修改了一个很小的debug,如果改变为2.1.0可能是新增了一些功能,如果改变为3.0.0可能是有很大幅度的修改,比如很多UI界面或功能的添加!
也就是版本号用于判断是否可以升级,而版本名用于显示給用户看!
(四)版本控制的文件位置
这个要区分你是用Eclipse开发还是Studio开发。
在Eclipse中版本控制的属性的位置是Manifest.xml中,如:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="3" android:versionName="1.2.1" package="com.example.updateDemo"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> 。。。 </application></manifest>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
上面表示android的第三个版本程序,版本名:1.2.1
在Android Studio呢?也是可以在Manifest.xml中定义versionCode和versionName,但是这里设置是无效的!
需要在程序的build.grade文件中设置,图解:
上面表示android的第三个版本程序,版本名:3.0.1
(五)版本信息的获取,代码
这里指的是获取运行中的程序的版本号,代码如下:
1
private String getVersionName() throws Exception{ PackageManager packageManager = getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0); Log.e("TAG","版本号"+packInfo.versionCode); Log.e("TAG","版本名"+packInfo.versionName); return packInfo.versionName;}
2
private int getVersionCode() throws Exception{ PackageManager packageManager = getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0); Log.e("TAG","版本号"+packInfo.versionCode); Log.e("TAG","版本名"+packInfo.versionName); return packInfo.versionCode;}
(六)版本更新中重要的代码块:
1.获取本程序的版本号和版本名,上面已经有了。
2.从服务器获取到一串json数据,里面包含最新程序的版本号和版本名、新版本信息等数据,需要自己解析到达对应的数据,因为服务器的数据不一样,所以这里的代码也不写!
3.检测是否更新的代码
public void checkVersion(View view) { if (getVersionCode() < serviceVersionCOde) { showDialogUpdate(); }else{ Toast.makeText(this,"当前已经是最新的版本",Toast.LENGTH_SHORT).show(); }}
4.弹出更新提示的对话框的代码
/** * 提示版本更新的对话框 */private void showDialogUpdate() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("版本升级"). setIcon(R.mipmap.ic_launcher). setMessage("发现新版本!请及时更新"). setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { loadNewVersionProgress(); } }). setNegativeButton("取消", null); AlertDialog alertDialog = builder.create(); alertDialog.show();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
5.下载新版本程序的代码
/** * 下载新版本程序,需要子线程 */private void loadNewVersionProgress() { final String uri="http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk"; final ProgressDialog pd; pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在下载更新"); pd.show(); new Thread(){ @Override public void run() { try { File file = getFileFromServer(uri, pd); sleep(3000); installApk(file); pd.dismiss(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_LONG).show(); e.printStackTrace(); } }}.start();}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
6.根据Uri网址获得apk文件对象的代码
/** * 从服务器获取apk文件的代码 * 传入网址uri,进度条对象即可获得一个File文件 * (要在子线程中执行哦) */public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception{ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); pd.setMax(conn.getContentLength()); InputStream is = conn.getInputStream(); long time= System.currentTimeMillis(); File file = new File(Environment.getExternalStorageDirectory(), time+"updata.apk"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len ; int total=0; while((len =bis.read(buffer))!=-1){ fos.write(buffer, 0, len); total+= len; pd.setProgress(total); } fos.close(); bis.close(); is.close(); return file; } else{ return null; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
7.安装apk文件的代码
/** * 安装apk */protected void installApk(File file) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent);}
上面有些代码块的格式是固定的,有些是可以根据实际情况进行修改。
二.程序更新的简单示例一
程序图:
点击“版本更新”按钮,弹出版本升级对话框。
说明:因为这里没有服务器,所以假设从服务器中获得版本号为3,而本程序的版本号为2,会弹出升级提示的对话框,点击升级后会链接到下载apk文件的地址,并实现下载,最后安装。(这里apk地址,很容易拿到,你上任何一个应用市场,在电脑上下载apk文件时,都会显示一个uri地址,这里的地址我用的是我之前在安智应用市场上传的一个小应用“wenzhi日历”)
代码:
(一)MainActivity的设计
package com.example.updateDemo;import android.app.AlertDialog;import android.app.ProgressDialog;import android.content.DialogInterface;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.net.Uri;import android.os.Environment;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.TextView;import android.widget.Toast;import java.io.BufferedInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.URL;import java.util.Currency;public class MainActivity extends AppCompatActivity { private static final int DOWN_ERROR = 505; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv_versionName = (TextView) findViewById(R.id.tv_versionName); try { tv_versionName.setText("版本名:" + getVersionName()); } catch (Exception e) { e.printStackTrace(); } } public void checkVersion(View view) { if (getVersionCode() < 3) { showDialogUpdate(); }else{ Toast.makeText(this,"当前已经是最新的版本",Toast.LENGTH_SHORT).show(); } } /** * 提示版本更新的对话框 */ private void showDialogUpdate() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("版本升级"). setIcon(R.mipmap.ic_launcher). setMessage("发现新版本!请及时更新"). setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { loadNewVersionProgress(); } }). setNegativeButton("取消", null); AlertDialog alertDialog = builder.create(); alertDialog.show(); } /** * 下载新版本程序 */ private void loadNewVersionProgress() { final String uri="http://www.apk.anzhi.com/data3/apk/201703/14/4636d7fce23c9460587d602b9dc20714_88002100.apk"; final ProgressDialog pd; pd = new ProgressDialog(this); pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); pd.setMessage("正在下载更新"); pd.show(); new Thread(){ @Override public void run() { try { File file = getFileFromServer(uri, pd); sleep(3000); installApk(file); pd.dismiss(); } catch (Exception e) { Toast.makeText(getApplicationContext(), "下载新版本失败", Toast.LENGTH_LONG).show(); e.printStackTrace(); } }}.start(); } /** * 安装apk */ protected void installApk(File file) { Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } /** * 从服务器获取apk文件的代码 * 传入网址uri,进度条对象即可获得一个File文件 * (要在子线程中执行哦) */ public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception{ if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ URL url = new URL(uri); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); pd.setMax(conn.getContentLength()); InputStream is = conn.getInputStream(); long time= System.currentTimeMillis(); File file = new File(Environment.getExternalStorageDirectory(), time+"updata.apk"); FileOutputStream fos = new FileOutputStream(file); BufferedInputStream bis = new BufferedInputStream(is); byte[] buffer = new byte[1024]; int len ; int total=0; while((len =bis.read(buffer))!=-1){ fos.write(buffer, 0, len); total+= len; pd.setProgress(total); } fos.close(); bis.close(); is.close(); return file; } else{ return null; } } private String getVersionName() throws Exception { PackageManager packageManager = getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0); Log.e("TAG", "版本号" + packInfo.versionCode); Log.e("TAG", "版本名" + packInfo.versionName); return packInfo.versionName; } private int getVersionCode() { try { PackageManager packageManager = getPackageManager(); PackageInfo packInfo = packageManager.getPackageInfo(getPackageName(), 0); Log.e("TAG", "版本号" + packInfo.versionCode); Log.e("TAG", "版本名" + packInfo.versionName); return packInfo.versionCode; } catch (Exception e) { e.printStackTrace(); } return 1; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- 203
- 204
- 205
- 206
- 207
- 208
(二)布局文件的设计
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><TextView android:padding="10dp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" android:id="@+id/tv_versionName" android:text="版本信息" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:text="检测更新" android:onClick="checkVersion" /></LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
布局文件是非常简单的,一个TextView显示版本名,一个Button检测本程序的版本号对比服务器的版本号,并提示是否升级。
(三)最后不要忘记添加网络和读写权限,
<uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
程序运行的效果:
![s1](http://img.blog.csdn.net/20170314161017525?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2VuemhpMjAxMDIzMjE=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
有些程序在刚启动的时候就检测版本升级,其实就是在onCreate方法里面执行。
三.程序更新的示例二
上面的程序升级不是覆盖原来的程序,因为我的两个程序的包名不一样的,第二个程序是直接安装在手机上面的,简单的说就是安装了两个程序。
如果要实现覆盖效果,需要包名相同并且签名相同。下面一个程序就实现了这种效果。
下面是两个我之前上线的程序,名字都是“wenzhi日历”,功能基本是一样的,但是一个是在“应用宝”应用市场上线的,版本是3.0;另一个是在“安智”应用市场上线的,版本是2.0;
两个版本都有版本更新功能,在2.0版本点击更新,可以下载3.0版本的程序,在3.0版本点击更新,提示:已经是最新版本!
注意:程序示例一的程序更新,下载下来的程序就是我在“安智”上线的应用程序,也就是本示例要用到的版本2.0的程序,点击版本更新可以直接覆盖原来的程序,得到版本3.0的程序。
效果:
还有一个需要注意的是如果手机安装了版本高的程序,想在安装同一个包名并且版本低的程序,是无法安装的(提示安装失败),只有先卸载高版本程序后才能安装低版本程序。
知识全程免费!欢迎大家阅读。