android - asynctask,progressbar和I / O. [英] android - asynctask, progressbar and I/O
问题描述
我很抱歉将相似的问题发布到我之前的问题上,但我无法解决这个问题。
I'm sorry for posting similar question to my previous one, but I just can't figure this out.
我一直在使用简单的ping示例我发现这里,只是想添加ProgressBar,但没有运气。我真的不明白发生了什么。
I've been using simple "ping" example I found here and just wanted to add ProgressBar but with no luck. I really don't understand what's going on.
所以,这很好用:
protected void onPreExecute() {
sb = new StringBuilder();
mPOut = new PipedOutputStream();
try {
mPIn = new PipedInputStream(mPOut);
mReader = new LineNumberReader(new InputStreamReader(mPIn));
} catch (IOException e) {
cancel(true);
}
//myBar.setVisibility(View.VISIBLE); -> PROBLEM!!!
}
protected Object doInBackground(Object... arg0) {
try {
process = Runtime.getRuntime().exec("ping -c 4 " + ipadd);
InputStream in = process.getInputStream();
OutputStream out = process.getOutputStream();
byte[] buffer = new byte[1024];
int count;
while ((count = in.read(buffer)) != -1) {
mPOut.write(buffer, 0, count);
String bs= new String(buffer);
publishProgress();
}
in.close();
out.close();
mPOut.close();
mPIn.close();
} catch (IOException e) {
}
return null;
}
protected void onPostExecute(Object result) {
myBar.setVisibility(View.INVISIBLE);
tv.setText(sb);
System.out.println(sb);
}
我从ping获得输出,myBar显然没有显示,因为它是隐形的第一名。
I get output from ping, myBar is obviously not diplayed since it is invisible in the first place.
如果我从标记为问题的行中删除注释(将进度条可见性设置为可见),我只是没有得到ping的输出。我似乎以某种方式弄乱了我的I / O或其他东西。 ProgressBar在结尾显示并隐藏,但没有输出。
If I remove the comment from the line I marked as a problem (set progress bar visibility to visible), I simply get no output from ping. I seems that it somehow messes up my I/O or something. ProgressBar is displayed and hidden at the end, but no output.
我真的无法弄清楚这一点,所以如果你有任何想法,我真的很感激任何帮助。
I really can't figure this out, so if you have any idea, I would really appreciate any help.
谢谢!!!
推荐答案
尝试以编程方式创建ProgressDialog在OnPreExecute()方法中,一旦在onPostExecute()中完成任务就将其解除。这是一个代码:
Try to create programmatically a ProgressDialog in OnPreExecute() method and dismiss it once task is finished in onPostExecute(). Here is a code:
add to onPreExcute:
super.onPreExecute();
ProgressDialog pDialog = new ProgressDialog(YourActivity.this);
pDialog.setMessage("Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
add to onPostExecute:
pDialog.dismiss();
这篇关于android - asynctask,progressbar和I / O.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!