Android自動更新

來源:互聯網
上載者:User

標籤:

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自動更新

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.