Ejemplo n.º 1
0
 @Override
 public void run() {
   super.run();
   try {
     initNofication();
     handlerTask();
     downLoadUtil = new Dow();
     int downSize =
         downLoadUtil.downloadUpdateFile(downloadUrl, saveFile, fileName, callback, context);
     if (downSize == downLoadUtil.getRealSize() && downSize != 0) {
       handler.sendEmptyMessage(downloadSuccess);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 2
0
        @Override
        public void handleMessage(Message msg) {
          if (msg.what == updateProgress) { // 更新下载进度
            int fileSize = downLoadUtil.getRealSize();
            int totalReadSize = downLoadUtil.getTotalSize();
            if (totalReadSize > 0) {
              float size = (float) totalReadSize * 100 / (float) fileSize;
              DecimalFormat format = new DecimalFormat("0.00");
              String progress = format.format(size);
              notificationViews.setTextViewText(R.id.progressTv, "已下了" + progress + "%");
              notificationViews.setProgressBar(R.id.progressBar, 100, (int) size, false);
              notification.contentView = notificationViews;
              notificationManager.notify(notificationID, notification);
            }
          } else if (msg.what == downloadSuccess) { // 下载完成
            notificationViews.setTextViewText(R.id.progressTv, "下载完成");
            notificationViews.setProgressBar(R.id.progressBar, 100, 100, false);
            notification.contentView = notificationViews;
            notification.tickerText = "下载完成";
            notificationManager.notify(notificationID, notification);
            if (timer != null && task != null) {
              timer.cancel();
              task.cancel();
              timer = null;
              task = null;
            }
            // 安装apk
            Uri uri = Uri.fromFile(new File(saveFile + "/" + fileName));
            Intent installIntent = new Intent(Intent.ACTION_VIEW);
            installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
            // PendingIntent 通知栏跳转
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, installIntent, 0);
            notification.flags = Notification.FLAG_AUTO_CANCEL;
            notification.contentIntent = pendingIntent;
            notification.contentView.setTextViewText(R.id.progressTv, "下载完成,点击安装!");
            notificationManager.notify(notificationID, notification);

          } else if (msg.what == downloadError) { // 下载失败
            if (timer != null && task != null) {
              timer.cancel();
              task.cancel();
              timer = null;
              task = null;
            }
            notificationManager.cancel(notificationID);
          }
        }