我使用AsyncTask更新水平进度对话框。没有错误。我唯一能解释的是ProgressDialog只在后台工作之后出现。
也就是说,单击按钮(依次执行以下操作:)
//within concerned OnClick Function of my main class:
ASYNC_TEST GH=new ASYNC_TEST(this,"something","something","url", "booga");
String response=GH.execute().get();
按钮键长时间保持按下状态(HTTP POST缓慢),然后突然显示进度对话框不超过一秒钟,快速完成100%并消失。
我粘贴了我的代码:
下面是扩展AsyncTask的类,它处理HTTP Post操作,应该更新进度条。
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.content.Context;
import android.util.Log;
public class ASYNC_TEST extends AsyncTask<Void, Integer, String> {
private String result = "";
private int ProgressStatus;
String username, password, THE_URL, YouSure;
private ProgressDialog pdb;
public ASYNC_TEST(Context ConT, String username, String password,String THE_URL, String YouSure)
{
Log.d("LOG_TAG", "Constructor");
this.username = username;
this.password = password;
this.THE_URL = THE_URL;
this.YouSure = YouSure;
pdb = new ProgressDialog(ConT);
}
@Override
protected void onPreExecute()
{
pdb.setCancelable(true);
pdb.setMessage("Logging In. Hold On a Sec...");
pdb.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdb.setProgress(0);
pdb.setMax(100);
pdb.show();
Log.d("LOG_TAG", "Pre-Execute");
super.onPreExecute();
}
@Override
protected void onPostExecute(String res)
{
Log.d("LOG_TAG", "Post-Execute");
pdb.dismiss();
super.onPostExecute(res);
}
@Override
protected void onProgressUpdate(Integer... progress)
{
Log.d("LOG_TAG", "Progress Update: " + progress[0]);
pdb.setProgress(progress[0]);
super.onProgressUpdate();
}
@Override
protected String doInBackground(Void... smt)
{
Log.d("LOG_TAG", "doInBackground: ");
try {
ProgressStatus = 0;
publishProgress(0);
//Stuff
ProgressStatus = 10;
publishProgress(10);
// More Stuff (HTTP POST)
ProgressStatus = 20;
publishProgress(10);
//....Finally...
ProgressStatus = 100;
publishProgress(100);
catch (Exception e)
{
e.printStackTrace();
}
return "";
}
}
记录数据:
05-18 15:19:52.773: D/LOG_TAG(1596): Constructor
05-18 15:19:52.822: D/LOG_TAG(1596): Pre-Execute
05-18 15:19:52.832: D/LOG_TAG(1596): doInBackground:
05-18 15:19:53.482: D/dalvikvm(1596): GC freed 13648 objects / 583400 bytes in 77ms
05-18 15:19:53.502: D/OpenSSLSessionImpl(1596): Freeing OpenSSL session
05-18 15:19:53.982: D/dalvikvm(1596): GC freed 17495 objects / 885728 bytes in 77ms
05-18 15:19:54.292: D/dalvikvm(1596): GC freed 10363 objects / 441600 bytes in 71ms
05-18 15:19:54.982: D/dalvikvm(1596): GC freed 6564 objects / 298808 bytes in 71ms
05-18 15:19:55.132: I/global(1596): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-18 15:19:55.212: D/dalvikvm(1596): GC freed 843 objects / 85392 bytes in 65ms
05-18 15:19:55.212: I/dalvikvm-heap(1596): Grow heap (frag case) to 4.120MB for 22148-byte allocation
05-18 15:19:55.282: D/dalvikvm(1596): GC freed 1 objects / 14776 bytes in 68ms
05-18 15:19:56.012: I/global(1596): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-18 15:19:56.092: D/dalvikvm(1596): GC freed 1758 objects / 193224 bytes in 67ms
05-18 15:19:56.172: D/dalvikvm(1596): GC freed 391 objects / 44792 bytes in 67ms
05-18 15:19:56.252: D/dalvikvm(1596): GC freed 411 objects / 62120 bytes in 66ms
05-18 15:19:56.252: I/dalvikvm-heap(1596): Grow heap (frag case) to 4.194MB for 54142-byte allocation
05-18 15:19:56.322: D/dalvikvm(1596): GC freed 1 objects / 36104 bytes in 69ms
05-18 15:19:56.413: D/dalvikvm(1596): GC freed 955 objects / 67536 bytes in 70ms
05-18 15:19:56.413: I/dalvikvm-heap(1596): Grow heap (frag case) to 4.237MB for 81208-byte allocation
05-18 15:19:56.483: D/dalvikvm(1596): GC freed 1 objects / 54152 bytes in 69ms
05-18 15:19:56.852: I/global(1596): Default buffer size used in BufferedReader constructor. It would be better to be explicit if an 8k-char buffer is required.
05-18 15:19:56.942: D/dalvikvm(1596): GC freed 1317 objects / 241880 bytes in 71ms
05-18 15:19:57.112: D/LOG_TAG(1596): Progress Update: 0
05-18 15:19:57.112: D/LOG_TAG(1596): Progress Update: 10
05-18 15:19:57.122: D/LOG_TAG(1596): Progress Update: 20
05-18 15:19:57.122: D/LOG_TAG(1596): Progress Update: 30
05-18 15:19:57.122: D/LOG_TAG(1596): Progress Update: 40
05-18 15:19:57.133: D/LOG_TAG(1596): Progress Update: 50
05-18 15:19:57.133: D/LOG_TAG(1596): Progress Update: 70
05-18 15:19:57.133: D/LOG_TAG(1596): Progress Update: 90
05-18 15:19:57.133: D/LOG_TAG(1596): Progress Update: 100
05-18 15:19:57.133: D/LOG_TAG(1596): Post-Execute
05-18 15:19:57.513: W/InputManagerService(51): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@44c5f5a0
答案 0 :(得分:1)
似乎延迟是由String response=GH.execute().get();
中的 get()方法引起的。它等待执行直到AsyncTask完成。
根据Javadoc:
get():如果需要等待计算完成,然后检索 结果。
我认为你应该简单地调用GH.execute();
来执行你的AsyncTask而不会影响UI线程。