1. 程式人生 > >AndroidAPP更新升級完整實現Demo(搭建本地伺服器) .

AndroidAPP更新升級完整實現Demo(搭建本地伺服器) .

市場上的Android應用都能夠自動提示升級更新,這裡就完整的來實現一下AndroidAPP,實現自動升級的功能。

效果圖:


具體如何實現,其實不難,先看看流程:



本地AndroidApp必須要先有一個版本號用於標識當前版本,再從伺服器獲取伺服器最新版本,進行相比較。

實現流程:

1、Manifest.xml新增聯網許可權,讀寫SD卡許可權,版本號versionCode和版本名versionName,其中versionCode用來比較版本使用的變數,versionName為用於顯示在介面上的版本字串。

[html] view plain copy print?
  1. <?
    xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.eric.androidupdatedemo"
  4. android:versionCode="1"
  5. android:versionName="01.00.01">
  6. <uses-permissionandroid:name='android.permission.INTERNET'/><!-- 聯網許可權 -->
  7. <uses-permissionandroid:name
    ="android.permission.WRITE_EXTERNAL_STORAGE"/><!-- 寫入SD卡許可權 -->
  8. <uses-permissionandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><!-- 在SD卡中建立和刪除檔案的許可權 -->
  9. <uses-sdk
  10. android:minSdkVersion="14"
  11. android:targetSdkVersion="14"/>
  12. <application
  13. android:allowBackup="true"
  14. android:icon
    ="@drawable/ic_launcher"
  15. android:label="@string/app_name"
  16. android:theme="@style/AppTheme">
  17. <activity
  18. android:name=".MainActivity"
  19. android:label="@string/app_name">
  20. <intent-filter>
  21. <actionandroid:name="android.intent.action.MAIN"/>
  22. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  23. </intent-filter>
  24. </activity>
  25. </application>
  26. </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佈局檔案,簡單的幾個控制元件。 [html] view plain copy print?
  1. <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:paddingBottom="@dimen/activity_vertical_margin"
  6. android:paddingLeft="@dimen/activity_horizontal_margin"
  7. android:paddingRight="@dimen/activity_horizontal_margin"
  8. android:paddingTop="@dimen/activity_vertical_margin"
  9. tools:context="com.eric.androidupdatedemo.MainActivity">
  10. <TextView
  11. android:id="@+id/textview_id"
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:text="@string/hello_world"/>
  15. <Button
  16. android:id="@+id/button_id"
  17. android:layout_width="fill_parent"
  18. android:layout_height="40dp"
  19. android:layout_below="@id/textview_id"
  20. android:textColor="#FFF"
  21. android:text="檢查更新"
  22. android:background="@drawable/buttonbg"
  23. />
  24. <ProgressBar
  25. android:id="@+id/progressBar_id"
  26. style="@android:style/Widget.ProgressBar.Horizontal"
  27. android:layout_width="match_parent"
  28. android:layout_height="40dp"
  29. android:layout_below="@id/button_id"
  30. android:layout_marginTop="10dp"
  31. android:visibility="gone"/>
  32. </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介面初始化,呼叫更新類的方法,重新整理頁面資料。 [java] view plain copy print?
  1. package com.eric.androidupdatedemo;  
  2. import java.io.File;  
  3. import android.app.Activity;  
  4. import android.app.AlertDialog;  
  5. import android.app.AlertDialog.Builder;  
  6. import android.content.Context;  
  7. import android.content.Intent;  
  8. import android.net.Uri;  
  9. import android.os.Bundle;  
  10. import android.os.Environment;  
  11. import android.os.Handler;  
  12. import android.os.Message;  
  13. import android.view.View;  
  14. import android.view.View.OnClickListener;  
  15. import android.view.Window;  
  16. import android.widget.Button;  
  17. import android.widget.ProgressBar;  
  18. import android.widget.TextView;  
  19. publicclass MainActivity extends Activity {  
  20. private TextView textView;  
  21. publicstaticint version,serverVersion;  
  22. publicstatic String versionName,serverVersionName,downloadResult;  
  23. private Button btn;  
  24. private ProgressBar proBar;  
  25. publicstatic receiveVersionHandler handler;  
  26. private UpdateManager manager = UpdateManager.getInstance();  
  27. @Override
  28. protectedvoid onCreate(Bundle savedInstanceState) {  
  29. super.onCreate(savedInstanceState);  
  30.         setContentView(R.layout.activity_main);  
  31.         textView = (TextView) findViewById(R.id.textview_id);  
  32.         btn = (Button) findViewById(R.id.button_id);  
  33.         proBar=(ProgressBar)findViewById(R.id.progressBar_id);  
  34.         Context c = this;  
  35.         version = manager.getVersion(c);  
  36.         versionName = manager.getVersionName(c);  
  37.         textView.setText("當前版本號:"+version+"\n"+"當前版本名:"+versionName);  
  38.         handler = new receiveVersionHandler();  
  39. //檢查更新按鈕點選事件
  40.         btn.setOnClickListener(new OnClickListener() {  
  41. @Override
  42. publicvoid onClick(View v) {  
  43.                 manager.compareVersion(MainActivity.this);  
  44.             }  
  45.         });  
  46.     }  
  47. publicclass receiveVersionHandler extends Handler{  
  48. @Override
  49. publicvoid handleMessage(Message msg) {  
  50.            proBar.setProgress(msg.arg1);  
  51.            proBar.setVisibility(R.id.button_id);  
  52.            textView.setText("下載進度:"+msg.arg1);  
  53. if(msg.arg1 == 100){  
  54.                Intent intent = new Intent(Intent.ACTION_VIEW);   
  55.                String path = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";  
  56.                intent.setDataAndType(Uri.fromFile(new File(path)),   
  57. "application/vnd.android.package-archive");     
  58.                startActivity(intent);  
  59.            }  
  60.            proBar.setVisibility(R.id.button_id);  
  61.         }  
  62.     }  
  63. }  
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檔案等方法。
[java] view plain copy print?
  1. package com.eric.androidupdatedemo;  
  2. import java.io.BufferedInputStream;  
  3. import java.io.BufferedOutputStream;  
  4. import java.io.File;  
  5. import java.io.FileOutputStream;  
  6. import java.net.HttpURLConnection;  
  7. import java.net.URL;  
  8. import org.json.JSONArray;  
  9. import org.json.JSONException;  
  10. import org.json.JSONObject;  
  11. import android.R.integer;  
  12. import android.app.AlertDialog;  
  13. import android.app.AlertDialog.Builder;  
  14. import android.content.Context;  
  15. import android.content.DialogInterface;  
  16. import android.os.Bundle;  
  17. import android.os.Environment;  
  18. import android.os.Handler;  
  19. import android.os.Looper;  
  20. import android.os.Message;  
  21. /* 
  22.  *@author Eric  
  23.  *@2015-11-7上午8:03:31 
  24.  */
  25. publicclass UpdateManager {  
  26. privatestatic UpdateManager manager = null;  
  27. private UpdateManager(){}  
  28. publicstatic UpdateManager getInstance(){  
  29.         manager = new UpdateManager();  
  30. return manager;  
  31.     }  
  32. //獲取版本號
  33. publicint getVersion(Context context){  
  34. int version = 0;  
  35. try {    
  36.             version = context.getPackageManager().getPackageInfo(    
  37. "com.eric.androidupdatedemo"0).versionCode;    
  38.         } catch (Exception e) {    
  39.              System.out.println("獲取版本號異常!");  
  40.         }    
  41. return version;  
  42.     }  
  43. //獲取版本名
  44. public String getVersionName(Context context){  
  45.         String versionName = null;  
  46. try {  
  47.             versionName = context.getPackageManager().getPackageInfo(  
  48. "com.eric.androidupdatedemo"0).versionName;  
  49.         } catch (Exception e) {  
  50.              System.out.println("獲取版本名異常!");  
  51.         }  
  52. return versionName;  
  53.     }  
  54. //獲取伺服器版本號
  55. public String getServerVersion(){  
  56.         String serverJson = null;  
  57. byte[] buffer = newbyte[128];  
  58. try {  
  59.             URL serverURL = new URL("http://192.168.226.106/ver.aspx");  
  60.             HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();  
  61.             BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());  
  62. int n = 0;  
  63. while((n = bis.read(buffer))!= -1){  
  64.                 serverJson = new String(buffer);  
  65.             }  
  66.         } catch (Exception e) {  
  67.             System.out.println("獲取伺服器版本號異常!"+e);  
  68.         }  
  69. return serverJson;  
  70.     }     
  71. //比較伺服器版本與本地版本彈出對話方塊
  72. publicboolean compareVersion(Context context){  
  73. final Context contextTemp = context;  
  74. new Thread(){  
  75. publicvoid run() {  
  76.                 Looper.prepare();  
  77.                 String serverJson = manager.getServerVersion();  
  78. //解析Json資料
  79. try {  
  80.                     JSONArray array = new JSONArray(serverJson);  
  81.                     JSONObject object = array.getJSONObject(0);  
  82.                     String getServerVersion = object.getString("version");  
  83.                     String getServerVersionName = object.getString("versionName");  
  84.                     MainActivity.serverVersion = Integer.parseInt(getServerVersion);  
  85.                     MainActivity.serverVersionName = getServerVersionName;  
  86. if(MainActivity.version < MainActivity.serverVersion){  
  87. //彈出一個對話方塊
  88.                         AlertDialog.Builder builder  = new Builder(contextTemp);    
  89.                         builder.setTitle("版本更新" ) ;    
  90.                         builder.setMessage("當前版本:"+MainActivity.versionName  
  91.                                 +"\n"+"伺服器版本:"+MainActivity.serverVersionName ) ;    
  92.                         builder.setPositiveButton("立即更新",new DialogInterface.OnClickListener() {    
  93. @Override
  94. publicvoid onClick(DialogInterface dialog, int arg1) {   
  95. //開啟執行緒下載apk
  96. new Thread(){  
  97. publicvoid run() {  
  98.                                            Looper.prepare();  
  99.                                            downloadApkFile(contextTemp);  
  100.                                            Looper.loop();  
  101.                                        };  
  102.                                    }.start();  
  103.                                }    
  104.                            });    
  105.                         builder.setNegativeButton("下次再說"null);    
  106.                         builder.show();  
  107.                     }else{  
  108.                         AlertDialog.Builder builder  = new Builder(contextTemp);    
  109.                         builder.setTitle("版本資訊" ) ;    
  110.                         builder.setMessage("當前已經是最新版本" ) ;    
  111.                         builder.setPositiveButton("確定",null);    
  112.                         builder.show();  
  113.                     }  
  114.                 } catch (JSONException e) {  
  115.                     e.printStackTrace();  
  116.                     System.out.println("獲取伺服器版本執行緒異常!"+e);  
  117.                 }  
  118.                 Looper.loop();  
  119.             };  
  120.         }.start();  
  121. returnfalse;  
  122.     }  
  123. //下載apk檔案
  124. publicvoid downloadApkFile(Context context){  
  125.         String savePath = Environment.getExternalStorageDirectory()+"/AndroidUpdateDemo.apk";  
  126.         String serverFilePath = "http://192.168.226.106/AndroidUpdateDemo.png";  
  127. try {  
  128. if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){    
  129.                 URL serverURL = new URL(serverFilePath);  
  130.                 HttpURLConnection connect = (HttpURLConnection) serverURL.openConnection();  
  131.                 BufferedInputStream bis = new BufferedInputStream(connect.getInputStream());  
  132.                 File apkfile = new File(savePath);  
  133.                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(apkfile));  
  134. int fileLength = connect.getContentLength();  
  135. int downLength = 0;  
  136. int progress = 0;  
  137. int n;  
  138. byte[] buffer = newbyte[1024];  
  139. while((n=bis.read(buffer, 0, buffer.length))!=-1){  
  140.                     bos.write(buffer, 0, n);  
  141.                     downLength +=n;  
  142.                     progress = (int) (((float) downLength / fileLength) * 100);  
  143.                     Message msg = new Message();  
  144.                     msg.arg1 = progress;  
  145.                     MainActivity.handler.sendMessage(msg);  
  146. //System.out.println("傳送"+progress);
  147.                 }  
  148.                 bis.close();  
  149.                 bos.close();  
  150.                 connect.disconnect();  
  151.             }   
  152.         } catch (Exception e) {  
  153.             System.out.println("下載出錯!"+e);  
  154.         }  
  155. /*AlertDialog.Builder builder  = new Builder(context);   
  156.         builder.setTitle("下載apk" ) ;   
  157.         builder.setMessage("正在下載" ) ;   
  158.         builder.setPositiveButton("確定",null);   
  159.         builder.show();*/
  160.     }  
  161. }  
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檔案。


上述操作結束後,伺服器端就完全搭好了,可以開始測試升級流程。