1. 程式人生 > >Android 應用強制更新策略

Android 應用強制更新策略

Android應用強制更新的用途十分廣泛,特別上剛上線的應用肯定會存在或多或少的bug,特別是涉及移動支付這一塊的內容,如果出錯了會造成比較大的損失,所以強制更新顯得尤為重要。

一般來說,強制更新的策略就是:
應用啟動時請求後臺,後臺傳送應用最新版本的資訊(包括應用版本號、名稱、更新內容說明、下載包的伺服器地址、是否強制更新的標誌位)等等。

下面我們就將根據以上思路來寫實現程式碼。

1.AndroidManifest配置版本資訊
在AndroidManifest.xml裡定義了每個Android apk的版本標識:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.demo" android:versionCode="1" android:versionName="1.0.0">
<application> </application> </manifest>

其中,package=”com.demo”是我們的包名;android:versionCode=”1”是版本代號,為整型數字;android:versionName=”1.0.0”是版本名稱,為字串,顯示給使用者看的。

當需要讀取AndroidManifest檔案中版本號和版本名稱的時候,用packageManager就可以簡單得到。程式碼如下所示:

public static int getVerCode(Context context) {
int verCode = -1;
try {
verCode = context.getPackageManager().getPackageInfo(
"com.demo", 0).versionCode;
} catch (NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return verCode;
}
public static String getVerName(Context context) {
String verName = ""
; try { verName = context.getPackageManager().getPackageInfo( "com.demo", 0).versionName; } catch (NameNotFoundException e) { Log.e(TAG, e.getMessage()); } return verName; }

2.進行版本檢查
在服務端放置最新版本的apk檔案,如:http://localhost/mydemo/demo.apk
同時,在服務端放置對應此apk的版本資訊呼叫介面或者檔案,如:http://localhost/mydemo/ver.json
ver.json中的內容為:
[{“appname”:”jtapp12”,”apkname”:”jtapp-12-updateapksamples.apk”,”verName”:1.0.1,”verCode”:2}]
然後,在手機客戶端上進行版本讀取和檢查:

private boolean getServerVer () {
try {
String verjson = NetworkTool.getContent(Config.UPDATE_SERVER
+ Config.UPDATE_VERJSON);
JSONArray array = new JSONArray(verjson);
if (array.length() > 0) {
JSONObject obj = array.getJSONObject(0);
try {
newVerCode = Integer.parseInt(obj.getString("verCode"));
newVerName = obj.getString("verName");
} catch (Exception e) {
newVerCode = -1;
newVerName = "";
return false;
}
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
return false;
}
return true;
}

比較伺服器和客戶端的版本,並進行更新操作。

if (getServerVerCode()) {
int vercode = Config.getVerCode(this); // 用到前面第一節寫的方法
if (newVerCode > vercode) {
doNewVersionUpdate(); // 更新新版本
} else {
notNewVersionShow(); // 提示當前為最新版本
}
}

呼叫方法:

private void notNewVersionShow() {
int verCode = Config.getVerCode(this);
String verName = Config.getVerName(this);
StringBuffer sb = new StringBuffer();
sb.append("當前版本:");
sb.append(verName);
sb.append(" Code:");
sb.append(verCode);
sb.append(",\n已是最新版,無需更新!");
Dialog dialog = new AlertDialog.Builder(Update.this).setTitle("軟體更新")
.setMessage(sb.toString())// 設定內容
.setPositiveButton("確定",// 設定確定按鈕
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
finish();
}
}).create();// 建立
// 顯示對話方塊
dialog.show();
}
private void doNewVersionUpdate() {
int verCode = Config.getVerCode(this);
String verName = Config.getVerName(this);
StringBuffer sb = new StringBuffer();
sb.append("當前版本:");
sb.append(verName);
sb.append(" Code:");
sb.append(verCode);
sb.append(", 發現新版本:");
sb.append(newVerName);
sb.append(" Code:");
sb.append(newVerCode);
sb.append(", 是否更新?");
Dialog dialog = new AlertDialog.Builder(Update.this)
.setTitle("軟體更新")
.setMessage(sb.toString())
// 設定內容
.setPositiveButton("更新",// 設定確定按鈕
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
pBar = new ProgressDialog(Update.this);
pBar.setTitle("正在下載");
pBar.setMessage("請稍候...");
pBar.setProgressStyle(ProgressDialog.STYLE_SPINNER);
downFile(Config.UPDATE_SERVER + Config.UPDATE_APKNAME);
}
})
.setNegativeButton("暫不更新",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// 點選"取消"按鈕之後退出程式
finish();
}
}).create();// 建立
// 顯示對話方塊
dialog.show();
}

更新下載:

void downFile(final String url) {
pBar.show();
new Thread() {
public void run() {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(url);
HttpResponse response;
try {
response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
FileOutputStream fileOutputStream = null;
if (is != null) {
File file = new File(
Environment.getExternalStorageDirectory(),
Config.UPDATE_SAVENAME);
fileOutputStream = new FileOutputStream(file);
byte[] buf = new byte[1024];
int ch = -1;
int count = 0;
while ((ch = is.read(buf)) != -1) {
fileOutputStream.write(buf, 0, ch);
count += ch;
if (length > 0) {
}
}
}
fileOutputStream.flush();
if (fileOutputStream != null) {
fileOutputStream.close();
}
down();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}

下載完成,通過handler通知主ui執行緒將下載對話方塊取消。

void down() {
handler.post(new Runnable() {
public void run() {
pBar.cancel();
update();
}
});
}

安裝應用

void update() {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment
.getExternalStorageDirectory(), Config.UPDATE_SAVENAME)),
"application/vnd.android.package-archive");
startActivity(intent);
}

如果你將apk應用釋出到market上,那麼,你會發現market內建了類似的模組,可以自動更新或者提醒你是否更新應用。那麼,對於你自己的應用需要自動更新的話,自己內建一個是不是更加方便了呢?本文提到的程式碼大多是在UpdateActivity.java中實現,為了能夠使更新過程更加友好,可以在最初launcher的Activity中建立一個執行緒,用來檢查服務端是否有更新。有更新的時候就啟動UpdateActivity,這樣的使用體驗更加平滑。

(待完成)