Android编程实现应用自动更新、下载、安装的方法

2019-12-10 19:05:05刘景俊

4. 下载模块

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();
        }
      });
}

5. 安装应用

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);
}