예제 #1
0
 @Override
 public void onDestroy() {
   // TODO Auto-generated method stub
   super.onDestroy();
   if (task != null) {
     task.exit();
   }
   Constant.isTaskRunning = false;
   android.os.Process.killProcess(android.os.Process.myPid());
   Log.e(TAG, "killProcess>>>loaderService");
 }
예제 #2
0
    /* (non-Javadoc)
     * @see android.os.Handler#handleMessage(android.os.Message)
     ***************************************************************************************
     *系统会自动回调方法,用于处理消息事件
     *Message 一般会包含消息的标志和消息的内容以及消息的处理器Handler
     */
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      switch (msg.what) {
        case Processing: // 下载时
          // 从消息中获取已经下载的数据长度
          if (LoaderActivity.progressBar != null) {
            LoaderActivity.progressBar.setMax((int) filesize); // 设置进度条最大值为文件的大小
          }
          int size = msg.getData().getInt("size");

          if (tmpsize == 0) {
            tmpsize = size;
            tmptime = System.currentTimeMillis();
          } else {
            // 更新速度
            if (size - tmpsize > 512000
                && ((long) (System.currentTimeMillis() - tmptime) / 1000) > 5) {
              speed =
                  (float)
                      ((size - tmpsize)
                          / (1024 * ((long) (System.currentTimeMillis() - tmptime) / 1000)));
              Log.e(TAG, "speed  " + speed + " KB");
              tmpsize = size;
              tmptime = System.currentTimeMillis();
            }
          }
          // 计算已经下载的百分比,要使用浮点值计算
          float num = (float) size / (float) filesize;
          int result = (int) (num * 100); // 把获得的浮点数换成整数
          if (LoaderActivity.tv_progress != null) {
            LoaderActivity.tv_progress.setText(
                mContext.getResources().getString(R.string.downloadprogress) + result + "%");
            if (speed != 0) {
              LoaderActivity.tv_speed.setText(
                  mContext.getResources().getString(R.string.downloadspeed) + speed + " KB/S");
            } else {
              LoaderActivity.tv_speed.setText(
                  mContext.getResources().getString(R.string.downloadspeed));
            }
          }
          if (LoaderActivity.progressBar != null) {
            LoaderActivity.progressBar.setProgress(size);
          }
          Log.i(TAG, "下载中Processing," + "result百分比是:" + result + "%");
          if (size == filesize) {
            // 当下载完成时,汇报信息
            initReportInfo(true);
            mHandler.sendEmptyMessage(10);
            if (LoaderActivity.txt_prompt != null) {
              LoaderActivity.txt_prompt.setText(
                  mContext.getResources().getString(R.string.downloadsuccess));
              LoaderActivity.tv_speed.setText(
                  mContext.getResources().getString(R.string.downloadspeed));
            }
            if (Constant.isTaskRunning) {
              Constant.isTaskRunning = false;
              File file1 = new File(Constant.path + File.separator + "update.zip");
              if (file1.exists()) file1.delete();
              File file2 = new File(Constant.filepath);
              boolean renameSuccess = file2.renameTo(file1);
              if (renameSuccess) {
                Intent intent = new Intent("SYSTEMUPDATE_DOWNLOAD_COMPLETE");
                intent.putExtra("path", Constant.path + File.separator + "update.zip");
                if (Constant.force != null && Constant.force.equals("1")) {
                  intent.putExtra("Force reboot", true);
                } else {
                  intent.putExtra("Force reboot", false);
                }
                mContext.sendBroadcast(intent);
                Log.e(TAG, "广播发送成功>>>" + Constant.path + File.separator + "update.zip");
              }
            }
            if (task != null) {
              task.exit();
            }
            Toast.makeText(
                    mContext,
                    mContext.getResources().getString(R.string.downloadsuccessed),
                    Toast.LENGTH_SHORT)
                .show();
          }
          break;

        case Failure: // 下载失败时
          Log.i(TAG, "收到Failure");
          initReportInfo(false);
          mHandler.sendEmptyMessage(10);
          if (task != null) {
            task.exit();
          }
          Constant.isTaskRunning = false;
          if (LoaderActivity.txt_prompt != null) {
            LoaderActivity.txt_prompt.setText(
                mContext.getResources().getString(R.string.downloadfailed));
            LoaderActivity.tv_progress.setVisibility(View.INVISIBLE);
            LoaderActivity.tv_speed.setVisibility(View.INVISIBLE);
            LoaderActivity.progressBar.setVisibility(View.INVISIBLE);
            LoaderActivity.btn.setVisibility(View.VISIBLE);
          }
          break;
        case 3:
          initSystemInfo();
          if (Constant.isTaskRunning) {

          } else {
            new Thread(
                    new Runnable() {

                      @Override
                      public void run() {
                        // TODO Auto-generated method stub
                        Log.e("hhh", ">>>>>>>>>>>>>");
                        boolean isOK = false;
                        if (serverAddress != null && !serverAddress.equals("")) {
                          currentServerAddress = serverAddress;
                          mHandler.sendEmptyMessage(9);
                          stringtmp = HttpClientRequestImpl.posttoserver(serverAddress, jsonString);
                          if (stringtmp != null && !stringtmp.equals("needless")) {
                            isOK = true;
                          }
                        }
                        // 如果添加地址不行再轮询服务器地址
                        if (!isOK) {
                          for (int i = 0; i < Constant.updatePaths.length; i++) {
                            // 提示检测哪一个服务器地址
                            currentServerAddress = Constant.updatePaths[i];
                            mHandler.sendEmptyMessage(9);
                            stringtmp =
                                HttpClientRequestImpl.posttoserver(
                                    Constant.updatePaths[i], jsonString);
                            if (stringtmp != null && !stringtmp.equals("needless")) {
                              break;
                            }
                          }
                        }
                        mHandler.sendEmptyMessage(6);
                      }
                    })
                .start();
          }
          break;
        case 8:
          initSystemInfo();
          if (Constant.isTaskRunning) {

          } else {
            new Thread(
                    new Runnable() {

                      @Override
                      public void run() {
                        // TODO Auto-generated method stub
                        Log.e("hhh", ">>>>>>>>>>>>>");
                        boolean isOK = false;
                        if (serverAddress != null && !serverAddress.equals("")) {
                          currentServerAddress = serverAddress;
                          mHandler.sendEmptyMessage(9);
                          stringtmp = HttpClientRequestImpl.posttoserver(serverAddress, jsonString);
                          if (stringtmp != null && !stringtmp.equals("needless")) {
                            isOK = true;
                          }
                        }
                        // 如果添加地址不行再轮询服务器地址
                        if (!isOK) {
                          for (int i = 0; i < Constant.updatePaths.length; i++) {
                            // 提示检测哪一个服务器地址
                            currentServerAddress = Constant.updatePaths[i];
                            mHandler.sendEmptyMessage(9);
                            stringtmp =
                                HttpClientRequestImpl.posttoserver(
                                    Constant.updatePaths[i], jsonString);
                            if (stringtmp != null && !stringtmp.equals("needless")) {
                              break;
                            }
                          }
                        }
                        mHandler.sendEmptyMessage(7);
                      }
                    })
                .start();
          }
          break;
        case 4:
          if (LoaderActivity.txt_prompt != null) {
            LoaderActivity.txt_prompt.setText(
                mContext.getResources().getString(R.string.systemupdatingprompt));
            LoaderActivity.tv_progress.setVisibility(View.VISIBLE);
            LoaderActivity.tv_speed.setVisibility(View.VISIBLE);
            LoaderActivity.progressBar.setVisibility(View.VISIBLE);
            LoaderActivity.btn.setVisibility(View.INVISIBLE);
          }
          if (Constant.downloadpath != null
              && !Constant.downloadpath.equals("")
              && !Constant.isTaskRunning) {
            Log.e(TAG, "开始地址下载!");
            task = new Downloadask(Constant.downloadpath, saveDir, 1); // 实例化下载任务,开始下载
            task.start();
            Toast.makeText(
                    mContext,
                    mContext.getResources().getString(R.string.systemupdating),
                    Toast.LENGTH_SHORT)
                .show();
          } else {
            //					Toast.makeText(getApplicationContext(), "下载地址获取失败!!!",
            // Toast.LENGTH_SHORT).show();
          }
          break;
        case 6:
          if (stringtmp != null && !stringtmp.equals("needless")) {
            stringtmp.trim();
            Constant.view = String.valueOf(stringtmp.charAt(0));
            Constant.force = String.valueOf(stringtmp.charAt(1));
            Constant.downloadpath = stringtmp.substring(2, stringtmp.length());
            Log.e("hhh", Constant.view);
            Log.e("hhh", Constant.force);
            Log.e("hhh", Constant.downloadpath);

            if (Constant.view.equals("0")) {
              mHandler.sendEmptyMessage(4);
            } else {
              Intent mIntent = new Intent(mContext, LoaderActivity.class);
              mIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              mContext.startActivity(mIntent);
              mHandler.sendEmptyMessage(4);
            }

            Log.e(TAG, Constant.view + "   " + Constant.downloadpath);
          } else {
            Constant.downloadpath = null;
            Constant.view = null;
          }
          break;
        case 7:
          if (LoaderActivity.btn != null) {
            LoaderActivity.btn.setEnabled(true);
          }
          if (stringtmp != null && !stringtmp.equals("needless")) {
            stringtmp.trim();
            Constant.view = String.valueOf(stringtmp.charAt(0));
            Constant.force = String.valueOf(stringtmp.charAt(1));
            Constant.downloadpath = stringtmp.substring(2, stringtmp.length());
            Log.e("hhh", Constant.view);
            Log.e("hhh", Constant.force);
            Log.e("hhh", Constant.downloadpath);

            mHandler.sendEmptyMessage(4);

            Log.e(TAG, Constant.view + "   " + Constant.downloadpath);
          } else {
            Constant.downloadpath = null;
            Constant.view = null;
            if (LoaderActivity.txt_prompt != null) {
              LoaderActivity.txt_prompt.setText(
                  mContext.getResources().getString(R.string.systemnoupdating));
              LoaderActivity.tv_progress.setVisibility(View.INVISIBLE);
              LoaderActivity.tv_speed.setVisibility(View.INVISIBLE);
              LoaderActivity.progressBar.setVisibility(View.INVISIBLE);
              LoaderActivity.btn.setVisibility(View.VISIBLE);
            }
          }
          break;
        case 9:
          if (LoaderActivity.txt_serveraddress != null) {
            LoaderActivity.txt_serveraddress.setText("服务器地址:" + currentServerAddress);
          }
          break;
        case 10:
          // 汇报升级结果
          new Thread(
                  new Runnable() {

                    @Override
                    public void run() {
                      // TODO Auto-generated method stub
                      if (jsonReportString != null && !jsonReportString.equals("")) {
                        HttpClientRequestImpl.posttoserver(
                            Constant.updateReportPaths, jsonReportString);
                      }
                    }
                  })
              .start();
          break;
        default:
          break;
      }
    }