標籤:
1.在後台維護一個Android的版本號碼,當每次進入APP的時候,在歡迎介面時,都去查詢這個最新的版本號碼.和當前APP的版本對比.
2.將最新的APP(最新版本號碼)放在伺服器上,並且提供一個下載功能的url(可以在後台維護一個上傳最新的APP的一個功能).
/** * 下載app * * @param url * @return */ public static Object downloadPdaLookUp(String action, final Context mContext) { String result = "[{\"note\":\"網路異常!請檢查網路!\",\"networkflag\":true}]"; if (!NetworkUtil.getNetworkStatus()) { return result; } String realurl = s_url_lookup + action; LogUtil.i(TAG, "" + realurl); try { HttpClient httpClient = new HttpClient(); UsernamePasswordCredentials creds = new UsernamePasswordCredentials( ActivityUtil.userName_c, ActivityUtil.password_c); LogUtil.i(TAG + "使用者", ActivityUtil.userName_c + ActivityUtil.password_c); httpClient.getState().setCredentials(AuthScope.ANY, creds); // 網路連接時間10秒 httpClient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 20 * 1000); // server端返回資料時間10秒 httpClient.getParams().setParameter( CoreConnectionPNames.SO_TIMEOUT, 50 * 1000); PostMethod method = new PostMethod(realurl); method.setDoAuthentication(true); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode = httpClient.executeMethod(method); if (statusCode == 200) { InputStream is; is = method.getResponseBodyAsStream(); String SDCard = Environment.getExternalStorageDirectory() + ""; String pathName = "/sdcard" + "/pda.apk";// 檔案儲存體路徑 // /storage/emulated/0/pda.apk File file = new File(pathName); FileOutputStream fos = new FileOutputStream(file); byte[] buf = new byte[1024]; int count = 0; while ((count = is.read(buf)) != -1) { fos.write(buf, 0, count); } fos.close(); is.close(); openFile(file, mContext); result = "[{\"note\":\"下載完成!\",\"status\":true}]"; return result; } } catch (ClientProtocolException e) { // e.printStackTrace(); result = "[{\"note\":\"網路通訊協定異常(ClientProtocolException)!\",\"networkflag\":true}]"; return result; } catch (IOException e) { // e.printStackTrace(); result = "[{\"note\":\"後台或網路(IOException)異常!\",\"networkflag\":true}]"; return result; } result = "[{\"note\":\"未知錯誤!請聯絡開發人員!\",\"networkflag\":true}]"; return result; }/** * 下載完pda後,自動開啟安裝程式. * * @param file * @param mContext */ private static void openFile(File file, final Context mContext) { // TODO Auto-generated method stub LogUtil.i("OpenFile", file.getName()); Intent intent = new Intent(); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive"); mContext.startActivity(intent); }
Android自動更新