现在的位置: 首页 > 综合 > 正文

Android 自动更新

2013年10月14日 ⁄ 综合 ⁄ 共 1743字 ⁄ 字号 评论关闭

private void downloadNewVerApkFile() {
        new Thread() {
            public void run() {
                HttpClient client = new DefaultHttpClient();
                String url = Requester.getUpdateUrl(ApkDownloadPath);
                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(),
                                downloadedApkName);
                        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();
                    }
                    installApk();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

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

抱歉!评论已关闭.