市場上的Android應用都能夠自動提示升級更新,這里就完整的來實現一下AndroidAPP,實現自動升級的功能。
Demo地址:http://download.csdn.net/detail/ericfantastic/9250609
效果圖:
具體如何實現,其實不難,先看看流程:
本地AndroidApp必須要先有一個版本號用於標識當前版本,再從服務器獲取服務器最新版本,進行相比較。
實現流程:
1、Manifest.xml添加聯網權限,讀寫SD卡權限,版本號versionCode和版本名versionName,其中versionCode用來比較版本使用的變量,versionName為用於顯示在界面上的版本字符串。
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.eric.androidupdatedemo"
- android:versionCode="1"
- android:versionName="01.00.01" >
- <uses-permission android:name='android.permission.INTERNET'/> <!-- 聯網權限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 寫入SD卡權限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 在SD卡中創建和刪除文件的權限 -->
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="14" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.eric.androidupdatedemo"
- android:versionCode="1"
- android:versionName="01.00.01" >
- <uses-permission android:name='android.permission.INTERNET'/> <!-- 聯網權限 -->
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <!-- 寫入SD卡權限 -->
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 在SD卡中創建和刪除文件的權限 -->
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="14" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
2、activity_main.xml布局文件,簡單的幾個控件。
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.eric.androidupdatedemo.MainActivity" >
- <TextView
- android:id="@+id/textview_id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <Button
- android:id="@+id/button_id"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_below="@id/textview_id"
- android:textColor="#FFF"
- android:text="檢查更新"
- android:background="@drawable/buttonbg"
- />
- <ProgressBar
- android:id="@+id/progressBar_id"
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:layout_below="@id/button_id"
- android:layout_marginTop="10dp"
- android:visibility="gone"/>
- </RelativeLayout>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingBottom="@dimen/activity_vertical_margin"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- tools:context="com.eric.androidupdatedemo.MainActivity" >
- <TextView
- android:id="@+id/textview_id"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="@string/hello_world" />
- <Button
- android:id="@+id/button_id"
- android:layout_width="fill_parent"
- android:layout_height="40dp"
- android:layout_below="@id/textview_id"
- android:textColor="#FFF"
- android:text="檢查更新"
- android:background="@drawable/buttonbg"
- />
- <ProgressBar
- android:id="@+id/progressBar_id"
- style="@android:style/Widget.ProgressBar.Horizontal"
- android:layout_width="match_parent"
- android:layout_height="40dp"
- android:layout_below="@id/button_id"
- android:layout_marginTop="10dp"
- android:visibility="gone"/>
- </RelativeLayout>
3、MainActivity.java界面初始化,調用更新類的方法,刷新頁面數據。
- package com.eric.androidupdatedemo;
- import java.io.File;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView textView;
- public static int version,serverVersion;
- public static String versionName,serverVersionName,downloadResult;
- private Button btn;
- private ProgressBar proBar;
- public static receiveVersionHandler handler;
- private UpdateManager manager = UpdateManager.getInstance();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView = (TextView) findViewById(R.id.textview_id);
- btn = (Button) findViewById(R.id.button_id);
- proBar=(ProgressBar)findViewById(R.id.progressBar_id);
- Context c = this;
- version = manager.getVersion(c);
- versionName = manager.getVersionName(c);
- textView.setText("當前版本號:"+version+"\n"+"當前版本名:"+versionName);
- handler = new receiveVersionHandler();
- //檢查更新按鈕點擊事件
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- manager.compareVersion(MainActivity.this);
- }
- });
- }
- public class receiveVersionHandler extends Handler{
- @Override
- public void handleMessage(Message msg) {
- proBar.setProgress(msg.arg1);
- proBar.setVisibility(R.id.button_id);
- textView.setText("下載進度:"+msg.arg1);
- if(msg.arg1 == 100){
- Intent intent = new Intent(Intent.ACTION_VIEW);
- String path = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";
- intent.setDataAndType(Uri.fromFile(new File(path)),
- "application/vnd.android.package-archive");
- startActivity(intent);
- }
- proBar.setVisibility(R.id.button_id);
- }
- }
- }
- package com.eric.androidupdatedemo;
- import java.io.File;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.Intent;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Message;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView textView;
- public static int version,serverVersion;
- public static String versionName,serverVersionName,downloadResult;
- private Button btn;
- private ProgressBar proBar;
- public static receiveVersionHandler handler;
- private UpdateManager manager = UpdateManager.getInstance();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView = (TextView) findViewById(R.id.textview_id);
- btn = (Button) findViewById(R.id.button_id);
- proBar=(ProgressBar)findViewById(R.id.progressBar_id);
- Context c = this;
- version = manager.getVersion(c);
- versionName = manager.getVersionName(c);
- textView.setText("當前版本號:"+version+"\n"+"當前版本名:"+versionName);
- handler = new receiveVersionHandler();
- //檢查更新按鈕點擊事件
- btn.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- manager.compareVersion(MainActivity.this);
- }
- });
- }
- public class receiveVersionHandler extends Handler{
- @Override
- public void handleMessage(Message msg) {
- proBar.setProgress(msg.arg1);
- proBar.setVisibility(R.id.button_id);
- textView.setText("下載進度:"+msg.arg1);
- if(msg.arg1 == 100){
- Intent intent = new Intent(Intent.ACTION_VIEW);
- String path = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";
- intent.setDataAndType(Uri.fromFile(new File(path)),
- "application/vnd.android.package-archive");
- startActivity(intent);
- }
- proBar.setVisibility(R.id.button_id);
- }
- }
- }
4、UpdateManager.java更新工具類,包含獲取本地版本信息,服務器版本信息,下載apk文件等方法。
- package com.eric.androidupdatedemo;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.R.integer;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.Message;
- /*
- *@author Eric
- *@2015-11-7上午8:03:31
- */
- public class UpdateManager {
- private static UpdateManager manager = null;
- private UpdateManager(){}
- public static UpdateManager getInstance(){
- manager = new UpdateManager();
- return manager;
- }
- //獲取版本號
- public int getVersion(Context context){
- int version = 0;
- try {
- version = context.getPackageManager().getPackageInfo(
- "com.eric.androidupdatedemo", 0).versionCode;
- } catch (Exception e) {
- System.out.println("獲取版本號異常!");
- }
- return version;
- }
- //獲取版本名
- public String getVersionName(Context context){
- String versionName = null;
- try {
- versionName = context.getPackageManager().getPackageInfo(
- "com.eric.androidupdatedemo", 0).versionName;
- } catch (Exception e) {
- System.out.println("獲取版本名異常!");
- }
- return versionName;
- }
- //獲取服務器版本號
- public String getServerVersion(){
- String serverJson = null;
- byte[] buffer = new byte[128];
- try {
- URL serverURL = new URL("http://192.168.226.106/ver.aspx");
- HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();
- BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());
- int n = 0;
- while((n = bis.read(buffer))!= -1){
- serverJson = new String(buffer);
- }
- } catch (Exception e) {
- System.out.println("獲取服務器版本號異常!"+e);
- }
- return serverJson;
- }
- //比較服務器版本與本地版本彈出對話框
- public boolean compareVersion(Context context){
- final Context contextTemp = context;
- new Thread(){
- public void run() {
- Looper.prepare();
- String serverJson = manager.getServerVersion();
- //解析Json數據
- try {
- JSONArray array = new JSONArray(serverJson);
- JSONObject object = array.getJSONObject(0);
- String getServerVersion = object.getString("version");
- String getServerVersionName = object.getString("versionName");
- MainActivity.serverVersion = Integer.parseInt(getServerVersion);
- MainActivity.serverVersionName = getServerVersionName;
- if(MainActivity.version < MainActivity.serverVersion){
- //彈出一個對話框
- AlertDialog.Builder builder = new Builder(contextTemp);
- builder.setTitle("版本更新" ) ;
- builder.setMessage("當前版本:"+MainActivity.versionName
- +"\n"+"服務器版本:"+MainActivity.serverVersionName ) ;
- builder.setPositiveButton("立即更新",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- //開啟線程下載apk
- new Thread(){
- public void run() {
- Looper.prepare();
- downloadApkFile(contextTemp);
- Looper.loop();
- };
- }.start();
- }
- });
- builder.setNegativeButton("下次再說", null);
- builder.show();
- }else{
- AlertDialog.Builder builder = new Builder(contextTemp);
- builder.setTitle("版本信息" ) ;
- builder.setMessage("當前已經是最新版本" ) ;
- builder.setPositiveButton("確定",null);
- builder.show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- System.out.println("獲取服務器版本線程異常!"+e);
- }
- Looper.loop();
- };
- }.start();
- return false;
- }
- //下載apk文件
- public void downloadApkFile(Context context){
- String savePath = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";
- String serverFilePath = "http://192.168.226.106/AndroidUpdateDemo.png";
- try {
- if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
- URL serverURL = new URL(serverFilePath);
- HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();
- BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());
- File apkfile = new File(savePath);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(apkfile));
- int fileLength = connect.getContentLength();
- int downLength = 0;
- int progress = 0;
- int n;
- byte[] buffer = new byte[1024];
- while((n=bis.read(buffer, 0, buffer.length))!=-1){
- bos.write(buffer, 0, n);
- downLength +=n;
- progress = (int) (((float) downLength / fileLength) * 100);
- Message msg = new Message();
- msg.arg1 = progress;
- MainActivity.handler.sendMessage(msg);
- //System.out.println("發送"+progress);
- }
- bis.close();
- bos.close();
- connect.disconnect();
- }
- } catch (Exception e) {
- System.out.println("下載出錯!"+e);
- }
- /*AlertDialog.Builder builder = new Builder(context);
- builder.setTitle("下載apk" ) ;
- builder.setMessage("正在下載" ) ;
- builder.setPositiveButton("確定",null);
- builder.show();*/
- }
- }
- package com.eric.androidupdatedemo;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import android.R.integer;
- import android.app.AlertDialog;
- import android.app.AlertDialog.Builder;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.os.Bundle;
- import android.os.Environment;
- import android.os.Handler;
- import android.os.Looper;
- import android.os.Message;
- /*
- *@author Eric
- *@2015-11-7上午8:03:31
- */
- public class UpdateManager {
- private static UpdateManager manager = null;
- private UpdateManager(){}
- public static UpdateManager getInstance(){
- manager = new UpdateManager();
- return manager;
- }
- //獲取版本號
- public int getVersion(Context context){
- int version = 0;
- try {
- version = context.getPackageManager().getPackageInfo(
- "com.eric.androidupdatedemo", 0).versionCode;
- } catch (Exception e) {
- System.out.println("獲取版本號異常!");
- }
- return version;
- }
- //獲取版本名
- public String getVersionName(Context context){
- String versionName = null;
- try {
- versionName = context.getPackageManager().getPackageInfo(
- "com.eric.androidupdatedemo", 0).versionName;
- } catch (Exception e) {
- System.out.println("獲取版本名異常!");
- }
- return versionName;
- }
- //獲取服務器版本號
- public String getServerVersion(){
- String serverJson = null;
- byte[] buffer = new byte[128];
- try {
- URL serverURL = new URL("http://192.168.226.106/ver.aspx");
- HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();
- BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());
- int n = 0;
- while((n = bis.read(buffer))!= -1){
- serverJson = new String(buffer);
- }
- } catch (Exception e) {
- System.out.println("獲取服務器版本號異常!"+e);
- }
- return serverJson;
- }
- //比較服務器版本與本地版本彈出對話框
- public boolean compareVersion(Context context){
- final Context contextTemp = context;
- new Thread(){
- public void run() {
- Looper.prepare();
- String serverJson = manager.getServerVersion();
- //解析Json數據
- try {
- JSONArray array = new JSONArray(serverJson);
- JSONObject object = array.getJSONObject(0);
- String getServerVersion = object.getString("version");
- String getServerVersionName = object.getString("versionName");
- MainActivity.serverVersion = Integer.parseInt(getServerVersion);
- MainActivity.serverVersionName = getServerVersionName;
- if(MainActivity.version < MainActivity.serverVersion){
- //彈出一個對話框
- AlertDialog.Builder builder = new Builder(contextTemp);
- builder.setTitle("版本更新" ) ;
- builder.setMessage("當前版本:"+MainActivity.versionName
- +"\n"+"服務器版本:"+MainActivity.serverVersionName ) ;
- builder.setPositiveButton("立即更新",new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int arg1) {
- //開啟線程下載apk
- new Thread(){
- public void run() {
- Looper.prepare();
- downloadApkFile(contextTemp);
- Looper.loop();
- };
- }.start();
- }
- });
- builder.setNegativeButton("下次再說", null);
- builder.show();
- }else{
- AlertDialog.Builder builder = new Builder(contextTemp);
- builder.setTitle("版本信息" ) ;
- builder.setMessage("當前已經是最新版本" ) ;
- builder.setPositiveButton("確定",null);
- builder.show();
- }
- } catch (JSONException e) {
- e.printStackTrace();
- System.out.println("獲取服務器版本線程異常!"+e);
- }
- Looper.loop();
- };
- }.start();
- return false;
- }
- //下載apk文件
- public void downloadApkFile(Context context){
- String savePath = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";
- String serverFilePath = "http://192.168.226.106/AndroidUpdateDemo.png";
- try {
- if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
- URL serverURL = new URL(serverFilePath);
- HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();
- BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());
- File apkfile = new File(savePath);
- BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(apkfile));
- int fileLength = connect.getContentLength();
- int downLength = 0;
- int progress = 0;
- int n;
- byte[] buffer = new byte[1024];
- while((n=bis.read(buffer, 0, buffer.length))!=-1){
- bos.write(buffer, 0, n);
- downLength +=n;
- progress = (int) (((float) downLength / fileLength) * 100);
- Message msg = new Message();
- msg.arg1 = progress;
- MainActivity.handler.sendMessage(msg);
- //System.out.println("發送"+progress);
- }
- bis.close();
- bos.close();
- connect.disconnect();
- }
- } catch (Exception e) {
- System.out.println("下載出錯!"+e);
- }
- /*AlertDialog.Builder builder = new Builder(context);
- builder.setTitle("下載apk" ) ;
- builder.setMessage("正在下載" ) ;
- builder.setPositiveButton("確定",null);
- builder.show();*/
- }
- }
具體實現都在UpdateManager類中,詳細說明一下,其中,
getVersion(Context context)、getVersionName(Context context)本地版本的獲取很簡單一看就懂;
getServerVersion()服務器版本的獲取:讀取服務器目錄下的ver.aspx文件,返回json字符串;
compareVersion(Context context)比較版本:啟動線程,讀取json字符串,並解析服務器版本賦值到serverVersion中,並且比較本地版本和服務器版本,如果低於服務器版本,就啟動新線程下載apk文件;
downloadApkFile(Context context)下載apk文件:設置文件保存路徑,服務器訪問路徑,通過HTTP協議下載文件,這里測試的時候,apk文件無法下載,故將服務器的apk修改擴展名為png格式,下載完成后保存為apk文件。
搭建本地Web服務器:
1、開啟功能:控制面板-> 程序-> 程序和功能 -> 打開或關閉Windows功能,將“Internet信息服務”下的所有功能都打開。
2、設置防火牆:控制面板-> 系統安全 -> Windows防火牆 -> 允許程序通過Windows防火牆,勾選“萬維網服務(HTTP)”。
3、以上步驟完成后,web服務器就搭好了,可以win+R,運行cmd,鍵入ipconfig /all ,查看本機IP地址,打開瀏覽器輸入地址ip地址測試一下,如果打開的是IIS7網頁,說明成功了。
然后就是將新版本的apk放到服務器目錄下,一般是系統盤目錄下C:\inetpub\wwwroot文件夾中。
放置兩個文件,一個是2.0版本的apk文件,一個是ver.aspx文件用於獲取服務器版本的json字符串,里面的內容為:[{"appname":"AndroidUpdateDemo","apkname":"AndroidUpdateDemo.apk","versionName":"02.00.01","version":"2"}] 。
由於測試時候apk文件無法直接下載,想了個辦法將apk文件擴展名改為png,在Android端下載完成后把它在保存為apk文件。
上述操作結束后,服務器端就完全搭好了,可以開始測試升級流程。