|
在Adnroid最底層,cp, mv 檔案常常都不知道是否完成因此想要增加processbar進度條
因為是最底層,也無法計算檔案大小
在GOOGLE搜尋下找到可以使用的方法
- package com.test;
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- public class Copy {
- private static final String PING_LOCALHOST = "ping localhost";
- /**
- * @param args
- * @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // TODO Auto-generated method stub
- ProgressBar progressBar = new ProgressBar();
- progressBar.showProgress();
- // first command
- runCommand(progressBar, PING_LOCALHOST, 25);
- runCommand(progressBar, PING_LOCALHOST, 50);
- runCommand(progressBar, PING_LOCALHOST, 75);
- runCommand(progressBar, PING_LOCALHOST, 100);
- }
- private static void runCommand(ProgressBar progressBar, String command, int barValue) throws InterruptedException {
- CopyThread copyThread = new Copy().new CopyThread(command);
- copyThread.start();
- // wait for thread to die
- copyThread.join();
- progressBar.updatePercent(barValue);
- }
- private class CopyThread extends Thread {
- private String cmd;
- public CopyThread(String command) {
- this.cmd = command;
- }
- @Override
- public void run() {
- // TODO Auto-generated method stub
- try {
- String line;
- Process p = Runtime.getRuntime().exec(cmd);
- BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
- while ((line = input.readLine()) != null) {
- System.out.println(line);
- }
- input.close();
- } catch (Exception err) {
- err.printStackTrace();
- }
- }
- }
- }
複製代碼
ProgressBar
- package com.test;
- import java.awt.Color;
- import java.awt.Font;
- import javax.swing.JFrame;
- import javax.swing.JProgressBar;
- public class ProgressBar {
- private JFrame progressFrame;
- private JProgressBar progressBar;
- /**
- * Create the application.
- */
- public ProgressBar() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- progressFrame = new JFrame();
- progressFrame.setTitle("Test progress bar");
- progressFrame.setResizable(false);
- progressFrame.setBounds(100, 100, 640, 79);
- progressFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
- progressFrame.getContentPane().setLayout(null);
- progressBar = new JProgressBar();
- progressBar.setFont(new Font("Tahoma", Font.PLAIN, 16));
- progressBar.setBackground(new Color(255, 255, 255));
- progressBar.setIndeterminate(false);
- progressBar.setStringPainted(true);
- progressBar.setForeground(new Color(0, 128, 0));
- progressBar.setBounds(0, 0, 634, 52);
- progressFrame.getContentPane().add(progressBar);
- }
- public void showProgress() {
- initialize();
- progressFrame.setVisible(true);
- }
- public void closeProgress() {
- progressFrame.dispose();
- progressFrame.setVisible(false);
- }
- public void updatePercent(int value) {
- progressBar.setValue(value);
- }
- }
複製代碼 文章出處
https://stackoverflow.com/questions/16940228/progress-bar-which-progresses-in-function-of-commands-line-or-copy-tasks
|
|