Android版本更新知识(检测、升级)总结
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中,如:
上面表示android的第三個版本程序,版本名:1.2.1
????????在Android Studio呢?也是可以在Manifest.xml中定義versionCode和versionName,但是這里設置是無效的!
需要在程序的build.grade文件中設置,圖解:
上面表示android的第三個版本程序,版本名:3.0.1
(五)版本信息的獲取,代碼
這里指的是獲取運行中的程序的版本號,代碼如下:
1
2
/** 獲取當前程序的版本號*/ private int getVersionCode() throws Exception{//獲取packagemanager的實例PackageManager packageManager = getPackageManager();//getPackageName()是你當前類的包名,0代表是獲取版本信息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() {// 這里的屬性可以一直設置,因為每次設置后返回的是一個builder對象AlertDialog.Builder builder = new AlertDialog.Builder(this);// 設置提示框的標題builder.setTitle("版本升級").// 設置提示框的圖標setIcon(R.mipmap.ic_launcher).// 設置要顯示的信息setMessage("發現新版本!請及時更新").// 設置確定按鈕setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//Toast.makeText(MainActivity.this, "選擇確定哦", 0).show();loadNewVersionProgress();//下載最新的版本程序}}).// 設置取消按鈕,null是什么都不做,并關閉對話框setNegativeButton("取消", null);// 生產對話框AlertDialog alertDialog = builder.create();// 顯示對話框alertDialog.show();}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(){@Overridepublic void run() {try {File file = getFileFromServer(uri, pd);sleep(3000);installApk(file);pd.dismiss(); //結束掉進度條對話框} catch (Exception e) {//下載apk失敗Toast.makeText(getApplicationContext(), "下載新版本失敗", Toast.LENGTH_LONG).show();e.printStackTrace();}}}.start(); }6.根據Uri網址獲得apk文件對象的代碼
/*** 從服務器獲取apk文件的代碼* 傳入網址uri,進度條對象即可獲得一個File文件* (要在子線程中執行哦)*/ public static File getFileFromServer(String uri, ProgressDialog pd) throws Exception{//如果相等的話表示當前的sdcard掛載在手機上并且是可用的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;} }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;@Overrideprotected 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();}}//檢測本程序的版本,這里假設從服務器中獲取到最新的版本號為3public void checkVersion(View view) {//如果檢測本程序的版本號小于服務器的版本號,那么提示用戶更新if (getVersionCode() < 3) {showDialogUpdate();//彈出提示版本更新的對話框}else{//否則吐司,說現在是最新的版本Toast.makeText(this,"當前已經是最新的版本",Toast.LENGTH_SHORT).show();}}/*** 提示版本更新的對話框*/private void showDialogUpdate() {// 這里的屬性可以一直設置,因為每次設置后返回的是一個builder對象AlertDialog.Builder builder = new AlertDialog.Builder(this);// 設置提示框的標題builder.setTitle("版本升級").// 設置提示框的圖標setIcon(R.mipmap.ic_launcher).// 設置要顯示的信息setMessage("發現新版本!請及時更新").// 設置確定按鈕setPositiveButton("確定", new DialogInterface.OnClickListener() {@Overridepublic void onClick(DialogInterface dialog, int which) {//Toast.makeText(MainActivity.this, "選擇確定哦", 0).show();loadNewVersionProgress();//下載最新的版本程序}}).// 設置取消按鈕,null是什么都不做,并關閉對話框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(){@Overridepublic void run() {try {File file = getFileFromServer(uri, pd);sleep(3000);installApk(file);pd.dismiss(); //結束掉進度條對話框} catch (Exception e) {//下載apk失敗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{//如果相等的話表示當前的sdcard掛載在手機上并且是可用的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 packageManager = getPackageManager();//getPackageName()是你當前類的包名,0代表是獲取版本信息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 packageManager = getPackageManager();//getPackageName()是你當前類的包名,0代表是獲取版本信息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;}}(二)布局文件的設計
<?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>布局文件是非常簡單的,一個TextView顯示版本名,一個Button檢測本程序的版本號對比服務器的版本號,并提示是否升級。
(三)最后不要忘記添加網絡和讀寫權限,
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>程序運行的效果:
???????有些程序在剛啟動的時候就檢測版本升級,其實就是在onCreate方法里面執行。
三.程序更新的示例二
???????上面的程序升級不是覆蓋原來的程序,因為我的兩個程序的包名不一樣的,第二個程序是直接安裝在手機上面的,簡單的說就是安裝了兩個程序。
???????如果要實現覆蓋效果,需要包名相同并且簽名相同。下面一個程序就實現了這種效果。
???????下面是兩個我之前上線的程序,名字都是“wenzhi日歷”,功能基本是一樣的,但是一個是在“應用寶”應用市場上線的,版本是3.0;另一個是在“安智”應用市場上線的,版本是2.0;
???????兩個版本都有版本更新功能,在2.0版本點擊更新,可以下載3.0版本的程序,在3.0版本點擊更新,提示:已經是最新版本!
???????注意:程序示例一的程序更新,下載下來的程序就是我在“安智”上線的應用程序,也就是本示例要用到的版本2.0的程序,點擊版本更新可以直接覆蓋原來的程序,得到版本3.0的程序。
效果:
代碼:這里就不展示了,其實更新功能代碼跟上面第一個程序差不多,其余的很多文件都是關于日歷的設計部分,需要的可以call我。
???????示例二的效果只是為了實現一種仿真升級的效果,大家沒有必要模仿。
???????升級的知識到這里就差不多了,兩個程序的源碼我現在都還是有的,包括上線的程序源碼,如果需要我都可以給你們。
wenzhi日歷(版本2.0)的“安智”應用市場的地址和圖:
http://sj.qq.com/myapp/detail.htm?apkName=com.liwenzhi_calendar.activity
wenzhi日歷(版本3.0)的“應用寶”應用市場的地址和圖:
http://www.anzhi.com/soft_2768407.html
???????還有一個需要注意的是如果手機安裝了版本高的程序,想在安裝同一個包名并且版本低的程序,是無法安裝的(提示安裝失敗),只有先卸載高版本程序后才能安裝低版本程序。
???????知識全程免費!歡迎大家閱讀。
總結
以上是生活随笔為你收集整理的Android版本更新知识(检测、升级)总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习(2580):提升开发效率的工具
- 下一篇: 工作61:动态绑定