TShopping

 找回密碼
 註冊
搜索
查看: 1465|回復: 0

[教學] Runtime.getRuntime().exec 如何使用 processbar

[複製鏈接]
發表於 2019-12-20 16:49:44 | 顯示全部樓層 |閱讀模式
 
Push to Facebook
在Adnroid最底層,cp, mv 檔案常常都不知道是否完成因此想要增加processbar進度條
因為是最底層,也無法計算檔案大小
在GOOGLE搜尋下找到可以使用的方法
  1. package com.test;

  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;

  4. public class Copy {

  5.     private static final String PING_LOCALHOST = "ping localhost";

  6.     /**
  7.      * @param args
  8.      * @throws InterruptedException
  9.      */
  10.     public static void main(String[] args) throws InterruptedException {
  11.         // TODO Auto-generated method stub
  12.         ProgressBar progressBar = new ProgressBar();
  13.         progressBar.showProgress();
  14.         // first command
  15.         runCommand(progressBar, PING_LOCALHOST, 25);
  16.         runCommand(progressBar, PING_LOCALHOST, 50);
  17.         runCommand(progressBar, PING_LOCALHOST, 75);
  18.         runCommand(progressBar, PING_LOCALHOST, 100);

  19.     }

  20.     private static void runCommand(ProgressBar progressBar, String command, int barValue) throws InterruptedException {
  21.         CopyThread copyThread = new Copy().new CopyThread(command);
  22.         copyThread.start();
  23.         // wait for thread to die
  24.         copyThread.join();
  25.         progressBar.updatePercent(barValue);
  26.     }

  27.     private class CopyThread extends Thread {
  28.         private String cmd;

  29.         public CopyThread(String command) {
  30.             this.cmd = command;
  31.         }

  32.         @Override
  33.         public void run() {
  34.             // TODO Auto-generated method stub
  35.             try {
  36.                 String line;
  37.                 Process p = Runtime.getRuntime().exec(cmd);
  38.                 BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
  39.                 while ((line = input.readLine()) != null) {
  40.                     System.out.println(line);
  41.                 }
  42.                 input.close();
  43.             } catch (Exception err) {
  44.                 err.printStackTrace();
  45.             }
  46.         }

  47.     }

  48. }
複製代碼

ProgressBar

  1. package com.test;

  2. import java.awt.Color;
  3. import java.awt.Font;

  4. import javax.swing.JFrame;
  5. import javax.swing.JProgressBar;

  6. public class ProgressBar {

  7.     private JFrame progressFrame;
  8.     private JProgressBar progressBar;

  9.     /**
  10.      * Create the application.
  11.      */
  12.     public ProgressBar() {
  13.         initialize();
  14.     }

  15.     /**
  16.      * Initialize the contents of the frame.
  17.      */
  18.     private void initialize() {

  19.         progressFrame = new JFrame();
  20.         progressFrame.setTitle("Test progress bar");
  21.         progressFrame.setResizable(false);
  22.         progressFrame.setBounds(100, 100, 640, 79);
  23.         progressFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
  24.         progressFrame.getContentPane().setLayout(null);

  25.         progressBar = new JProgressBar();
  26.         progressBar.setFont(new Font("Tahoma", Font.PLAIN, 16));
  27.         progressBar.setBackground(new Color(255, 255, 255));
  28.         progressBar.setIndeterminate(false);
  29.         progressBar.setStringPainted(true);
  30.         progressBar.setForeground(new Color(0, 128, 0));
  31.         progressBar.setBounds(0, 0, 634, 52);

  32.         progressFrame.getContentPane().add(progressBar);
  33.     }

  34.     public void showProgress() {
  35.         initialize();
  36.         progressFrame.setVisible(true);
  37.     }

  38.     public void closeProgress() {
  39.         progressFrame.dispose();
  40.         progressFrame.setVisible(false);
  41.     }

  42.     public void updatePercent(int value) {
  43.         progressBar.setValue(value);
  44.     }
  45. }
複製代碼
文章出處

https://stackoverflow.com/questions/16940228/progress-bar-which-progresses-in-function-of-commands-line-or-copy-tasks

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-3-28 20:00 , Processed in 0.086672 second(s), 23 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表