[英]Show progressbar while executing shell commands
Im trying to make a progress bar appear during the time that I copy files (using shell commands). 我试图使进度条出现在我复制文件的过程中(使用shell命令)。 Here is my code: 这是我的代码:
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();
I have multiple different processes that i need to execute, so how can i make a progress dialog appear at the start and get dismissed when the last file has successfully finished copying. 我需要执行多个不同的过程,因此,如何使进度对话框显示在开始位置,并在最后一个文件成功完成复制后将其关闭。 I tried showing the dialog before procccess1 and dismiss after the last process, but that doesnt work. 我尝试在procccess1之前显示该对话框,并在最后一个过程后关闭,但这不起作用。 Thanks. 谢谢。
Apparently i need to wrap it in a Thread. 显然我需要将其包装在线程中。 Can someone show me how i would do this? 有人可以告诉我我该怎么做吗?
Several issues are here in your original code. 您的原始代码中有几个问题。 The code below handles the running of the processes on a background thread and checks their results using the waitFor call. 下面的代码处理进程在后台线程上的运行,并使用waitFor调用检查其结果。 All of this is really moot anyway because you cannot copy to /system without root. 无论如何,所有这些实际上都是没有实际意义的,因为没有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();
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.