执行shell命令时显示进度条



我试图在复制文件期间(使用shell命令)显示进度条。这是我的代码:

    copyProgress = new ProgressDialog(Activ.this);
    copyProgress.setMessage("Copying");
    copyProgress.show();  
    Process process1 = Runtime.getRuntime().exec(new String[] {"cp /sdcard/file /system"});
    Process process2 = Runtime.getRuntime().exec(new String[] {"cp /sdcard/file2 /system"});
    .......
    copyProgress.dismiss();

我有多个不同的进程需要执行,所以我如何在开始时显示进度对话框,并在最后一个文件成功完成复制时被解雇。我试着在procaccess1之前显示对话框,并在最后一个过程之后关闭,但这不起作用。谢谢

显然我需要用线把它包起来。有人能告诉我该怎么做吗?

原始代码中存在几个问题。下面的代码处理后台线程上进程的运行,并使用waitFor调用检查其结果。无论如何,所有这些都是没有实际意义的,因为没有root就无法复制到/system。

{   
   copyProgress = new ProgressDialog(Activ.this);
   copyProgress.setMessage("Copying");
   copyProgress.show();  
   new DoShellScriptyThingsAsyncThread().execute();
}
private class DoShellScriptyThingsAsyncThread extends AsyncTask<Void,Void,Void>
{
    @Override
    protected Void doInBackground(Void... params) {
        doCopy("file");
        publishProgress();
        doCopy("file2");
        publishProgress()
        return null;
    }
    private void  doCopy(String filename)
    {
        try    
    {            
        Process proc = Runtime.getRuntime().exec(new String[] {"cp /sdcard/"  +filename +" /system"});
        InputStream stdin = proc.getInputStream();
        InputStreamReader isr = new InputStreamReader(stdin);
        BufferedReader br = new BufferedReader(isr);
        String line = null;
        System.out.println("<OUTPUT>");
        while ( (line = br.readLine()) != null)
            System.out.println(line);
        System.out.println("</OUTPUT>");
        int exitVal = proc.waitFor();            
        System.out.println("Process exitValue: " + exitVal);
    } catch (Throwable t)
      {
        t.printStackTrace();
        //Now do some thing if this fails - which it will because you are trying
        // to copy something to system
      }
}
    @Override
    protected void onProgressUpdate(Void... updateInteger)
    {
        //Update your progress dialog here!
        copyProgress.incrementProgress(5000);
    }
  }
    @Override
    protected void onPostExecute(Void result)
    {
        //Make your progress dialog go away here
        copyProgress.dismiss();
    }
};

相关内容

  • 没有找到相关文章

最新更新