Android應用開發(fā)的版本更新檢測升級功能實現(xiàn)示例
一.版本的基礎(chǔ)知識
版本控制的屬性包括versionCode和versionName。
(一)versionCode
版本號(versionCode)是相對比較重要的一個屬性。versionCode是一個Integer類型的值。
所以大家在設置的時候,不要將versionCode設置的太大,最好不要超過Integer的取值范圍(當然一般也是不會超過的),一般大家在發(fā)布自己的第一個應用到市場的時候,版本取值為1(versionCode=1),這也是目前典型和普遍的做法。然后,每次發(fā)布更新版本時可以遞增versionCode的值。
(二)versionName
版本名(versionName)一個值為String類型的屬性,一般和VersionCode成對出現(xiàn)。
VersionCode是方便程序開發(fā)者運行和維護Application而設置的一個有效的值。versionName是一個版本的描述,給用戶看的,也是用戶放在各個第3方平臺上提供給使用者看的一個版本名,可以說是對VersionCode的解釋和描述。一般格式可以為:1.1.2。(major.minor.point)的形式。
(三)版本控制小結(jié)
版本號(versionCode)是用于判斷是否升級的,一般每次版本更新,版本號加一。
如果獲取服務器上的版本號比檢測到本程序的版本號高,那么提示升級。
版本名(versionName)用于顯示版本改變的幅度大小,比如從2.0.1改變?yōu)?.0.2可能只是修改了一個很小的debug,如果改變?yōu)?.1.0可能是新增了一些功能,如果改變?yōu)?.0.0可能是有很大幅度的修改,比如很多UI界面或功能的添加!
也就是版本號用于判斷是否可以升級,而版本名用于顯示給用戶看!
(四)版本控制的文件位置
這個要區(qū)分你是用Eclipse開發(fā)還是Studio開發(fā)。
在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>
上面表示android的第三個版本程序,版本名:1.2.1
在Android Studio呢?也是可以在Manifest.xml中定義versionCode和versionName,但是這里設置是無效的!
需要在程序的build.grade文件中設置,圖解:
上面表示android的第三個版本程序,版本名:3.0.1
(五)版本信息的獲取,代碼
這里指的是獲取運行中的程序的版本號,代碼如下:
1
/* * 獲取當前程序的版本名 */ 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; }
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.獲取本程序的版本號和版本名
上面已經(jīng)有了
2.從服務器獲取到一串json數(shù)據(jù)
里面包含最新程序的版本號和版本名、新版本信息等數(shù)據(jù),需要自己解析到達對應的數(shù)據(jù),因為服務器的數(shù)據(jù)不一樣,所以這里的代碼也不寫!
3.檢測是否更新的代碼
//對比本程序的版本號和最新程序的版本號 public void checkVersion(View view) {//按鈕! //如果檢測本程序的版本號小于服務器的版本號,那么提示用戶更新 if (getVersionCode() < serviceVersionCOde) { showDialogUpdate();//彈出提示版本更新的對話框 }else{ //否則吐司,說現(xiàn)在是最新的版本 Toast.makeText(this,"當前已經(jīng)是最新的版本",Toast.LENGTH_SHORT).show(); } }
4.彈出更新提示的對話框的代碼
/** * 提示版本更新的對話框 */ private void showDialogUpdate() { // 這里的屬性可以一直設置,因為每次設置后返回的是一個builder對象 AlertDialog.Builder builder = new AlertDialog.Builder(this); // 設置提示框的標題 builder.setTitle("版本升級"). // 設置提示框的圖標 setIcon(R.mipmap.ic_launcher). // 設置要顯示的信息 setMessage("發(fā)現(xiàn)新版本!請及時更新"). // 設置確定按鈕 setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Toast.makeText(MainActivity.this, "選擇確定哦", 0).show(); loadNewVersionProgress();//下載最新的版本程序 } }). // 設置取消按鈕,null是什么都不做,并關(guān)閉對話框 setNegativeButton("取消", null); // 生產(chǎn)對話框 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(){ @Override public void run() { try { File file = getFileFromServer(uri, pd); sleep(3000); installApk(file); pd.dismiss(); //結(jié)束掉進度條對話框 } catch (Exception e) { //下載apk失敗 Toast.makeText(getApplicationContext(), "下載新版本失敗", Toast.LENGTH_LONG).show(); e.printStackTrace(); } }}.start(); }
6.根據(jù)Uri網(wǎng)址獲得apk文件對象的代碼
/** * 從服務器獲取apk文件的代碼 * 傳入網(wǎng)址uri,進度條對象即可獲得一個File文件 * (要在子線程中執(zhí)行哦) */ 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();//當前時間的毫秒數(shù) 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(); //執(zhí)行動作 intent.setAction(Intent.ACTION_VIEW); //執(zhí)行的數(shù)據(jù)類型 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); }
上面有些代碼塊的格式是固定的,有些是可以根據(jù)實際情況進行修改。
二.程序更新的簡單示例一
程序圖:
點擊“版本更新”按鈕,彈出版本升級對話框。
說明:因為這里沒有服務器,所以假設從服務器中獲得版本號為3,而本程序的版本號為2,會彈出升級提示的對話框,點擊升級后會鏈接到下載apk文件的地址,并實現(xiàn)下載,最后安裝。
(這里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(); } } //檢測本程序的版本,這里假設從服務器中獲取到最新的版本號為3 public void checkVersion(View view) { //如果檢測本程序的版本號小于服務器的版本號,那么提示用戶更新 if (getVersionCode() < 3) { showDialogUpdate();//彈出提示版本更新的對話框 }else{ //否則吐司,說現(xiàn)在是最新的版本 Toast.makeText(this,"當前已經(jīng)是最新的版本",Toast.LENGTH_SHORT).show(); } } /** * 提示版本更新的對話框 */ private void showDialogUpdate() { // 這里的屬性可以一直設置,因為每次設置后返回的是一個builder對象 AlertDialog.Builder builder = new AlertDialog.Builder(this); // 設置提示框的標題 builder.setTitle("版本升級"). // 設置提示框的圖標 setIcon(R.mipmap.ic_launcher). // 設置要顯示的信息 setMessage("發(fā)現(xiàn)新版本!請及時更新"). // 設置確定按鈕 setPositiveButton("確定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Toast.makeText(MainActivity.this, "選擇確定哦", 0).show(); loadNewVersionProgress();//下載最新的版本程序 } }). // 設置取消按鈕,null是什么都不做,并關(guān)閉對話框 setNegativeButton("取消", null); // 生產(chǎn)對話框 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(); //結(jié)束掉進度條對話框 } catch (Exception e) { //下載apk失敗 Toast.makeText(getApplicationContext(), "下載新版本失敗", Toast.LENGTH_LONG).show(); e.printStackTrace(); } }}.start(); } /** * 安裝apk */ protected void installApk(File file) { Intent intent = new Intent(); //執(zhí)行動作 intent.setAction(Intent.ACTION_VIEW); //執(zhí)行的數(shù)據(jù)類型 intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); startActivity(intent); } /** * 從服務器獲取apk文件的代碼 * 傳入網(wǎng)址uri,進度條對象即可獲得一個File文件 * (要在子線程中執(zhí)行哦) */ 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();//當前時間的毫秒數(shù) 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檢測本程序的版本號對比服務器的版本號,并提示是否升級。
(三)最后不要忘記添加網(wǎng)絡和讀寫權(quán)限,
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
程序運行的效果:
有些程序在剛啟動的時候就檢測版本升級,其實就是在onCreate方法里面執(zhí)行。
三.程序更新的示例二
上面的程序升級不是覆蓋原來的程序,因為我的兩個程序的包名不一樣的,第二個程序是直接安裝在手機上面的,簡單的說就是安裝了兩個程序。
如果要實現(xiàn)覆蓋效果,需要包名相同并且簽名相同。下面一個程序就實現(xiàn)了這種效果。
下面是兩個我之前上線的程序,名字都是“wenzhi日歷”,功能基本是一樣的,但是一個是在“應用寶”應用市場上線的,版本是3.0;另一個是在“安智”應用市場上線的,版本是2.0;
兩個版本都有版本更新功能,在2.0版本點擊更新,可以下載3.0版本的程序,在3.0版本點擊更新,提示:已經(jīng)是最新版本!
注意:程序示例一的程序更新,下載下來的程序就是我在“安智”上線的應用程序,也就是本示例要用到的版本2.0的程序,點擊版本更新可以直接覆蓋原來的程序,得到版本3.0的程序。
效果:
代碼:這里就不展示了,其實更新功能代碼跟上面第一個程序差不多,其余的很多文件都是關(guān)于日歷的設計部分。
示例二的效果只是為了實現(xiàn)一種仿真升級的效果,大家沒有必要模仿。
wenzhi日歷(版本2.0)
wenzhi日歷(版本3.0)
還有一個需要注意的是如果手機安裝了版本高的程序,想在安裝同一個包名并且版本低的程序,是無法安裝的(提示安裝失?。?,只有先卸載高版本程序后才能安裝低版本程序。
以上就是Android應用開發(fā)的版本更新檢測升級功能實現(xiàn)示例的詳細內(nèi)容,更多關(guān)于Android應用開發(fā)的版本更新檢測升級的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android開發(fā)實現(xiàn)的文本折疊點擊展開功能示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的文本折疊點擊展開功能,涉及Android界面布局與屬性控制相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Android實戰(zhàn)教程第七篇之如何在內(nèi)存中存儲用戶名和密碼
這篇文章主要為大家詳細介紹了Android如何實現(xiàn)在內(nèi)存中存儲用戶名和密碼的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11