接着我们要看createThread方法
/*** * 开线程下载 */ public void createThread() { /*** * 更新UI */ final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { switch (msg.what) { case DOWN_OK: // 下载完成,点击安装 Uri uri = Uri.fromFile(FileUtil.updateFile); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "application/vnd.android.package-archive"); pendingIntent = PendingIntent.getActivity( UpdateService.this, 0, intent, 0); notification.setLatestEventInfo(UpdateService.this, app_name, "下载成功,点击安装", pendingIntent); notificationManager.notify(notification_id, notification); stopSelf(); break; case DOWN_ERROR: notification.setLatestEventInfo(UpdateService.this, app_name, "下载失败", pendingIntent); break; default: stopSelf(); break; } } }; final Message message = new Message(); new Thread(new Runnable() { @Override public void run() { try { long downloadSize = downloadUpdateFile(down_url, FileUtil.updateFile.toString()); if (downloadSize > 0) { // 下载成功 message.what = DOWN_OK; handler.sendMessage(message); } } catch (Exception e) { e.printStackTrace(); message.what = DOWN_ERROR; handler.sendMessage(message); } } }).start(); }这个方法有点小多,不过我想大家都看的明白,我在这里简单说名一下:首先我们创建一个handler用于检测最后下载ok还是not ok.
下面我们开启了线程进行下载数据。