如何超时的AsyncTask和解雇ProgressDialog? [英] How to timeout Asynctask and dismiss ProgressDialog?
问题描述
我有一个问题,并且希望你能帮助我。
我有一个AsyncTask的,如果我preSS在我mainactivity一个按钮,开始上传数据。
它的工作原理,除非我有一个缓慢的互联网连接的罚款。
该AsyncTask的启动progressdialog,如果我有一个连接速度慢,停的AsyncTask但Progressdialog不会消失,因为它从来没有达到过onPostExecute。
现在我想实现一个超时,但不能找到一种方法,让progressdialog驳回做到这一点。
下面是我的code:
@覆盖
在preExecute保护无效(){
super.on preExecute();
时间= System.currentTimeMillis的(); Log.d(TAG,PBAR:帽子gestartet);
如果(MainActivity.MessungStart ==真)
{
ConnectionTask.dialog =新ProgressDialog(MainActivity.context); ConnectionTask.dialog.setMessage(回到名单Daten werden hochgeladen ......);
dialog.setCanceledOnTouchOutside(假);
ConnectionTask.dialog.show();
}
}保护无效doInBackground(字符串参数... args){ MUSER = MainActivity.username;
mPassword = MainActivity.password;
Log.d(TAG,异步:用户=+ MUSER +密码=+ mPassword); TIMER T =新的Timer();
TimerTask的TK =新的TimerTask(){ @覆盖
公共无效的run(){
超时= TRUE;
}
};
t.schedule(TK,100);
如果(超时==真)
{
Log.d(TAG,超时=真);
onPostExecute(NULL);
} 尝试{
认证();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
{
尝试{
送出数据();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
} 如果(温度!= NULL)
{
尝试{
ReceiveMessageState();
}赶上(IOException异常五){
// TODO自动生成catch块
e.printStackTrace();
}
}
Sendungfertig =真;
如果(MainActivity.asyncIsCanceled ==真)
{
如果(ConnectionTask.dialog.isShowing()==真)
{
ConnectionTask.dialog.dismiss();
}
}
isCancelled();
返回null;
}@覆盖
保护无效onPostExecute(虚空V){
super.onPostExecute(五);
Log.d(TAG,PBAR:Sollte enden);
如果(MessageChecked ==真)
{
如果(ConnectionTask.dialog.isShowing()==真)
{
ConnectionTask.dialog.dismiss();
}
如果(isCancelled()==真)
{
如果(ConnectionTask.dialog.isShowing()==真)
{
ConnectionTask.dialog.dismiss();
}
MessageChecked = FALSE;
MainActivity.MessungStart = FALSE;
}
}
}
不要在preExecute(),onPostExecute(结果),doInBackground(参数...),onProgressUpdate(进展...)手动。
打电话
块引用>而不是做
如果(超时==真)
{
Log.d(TAG,超时=真);
onPostExecute(NULL); //这应该被删除只是
返回NULL
,而不是在那里。这将返回控制onPostExecute()
在这里你可以关闭你的对话框
。修改
我觉得你正在它也与
的TimerTask
复杂在这种情况下,因为一切都将继续运行。你可以做的是使用一个while循环
和计数器任何时候你想要的。因此,像长WAITTIME = 1000; //或者任何你想要的超时时间长度为
长curWaitTime = 0;
而(超时&安培;!&安培; curWaitTime< WAITTIME)
{
//把你的网络/文件code在这里
//如果那么数据结束,你可以设置超时为true
curWaitTime + = 100; //或其他一些价值增加
视频下载(100);
}
返回null;I've got a problem and hope you can help me. I've got an Asynctask, which starts uploading data if I press a button in my mainactivity. It works fine except if I've got a slow internet connection. The Asynctask starts a progressdialog and if I've got a slow connection, Asynctask stops but the Progressdialog doesn't disappear because it never reached onPostExecute.
Now I'm trying to implement a timeout but can't find a way, so that the progressdialog dismisses to do this.
Here is my code:
@Override protected void onPreExecute() { super.onPreExecute(); time = System.currentTimeMillis(); Log.d(TAG, "PBar: Hat gestartet"); if (MainActivity.MessungStart == true) { ConnectionTask.dialog = new ProgressDialog(MainActivity.context); ConnectionTask.dialog.setMessage("Daten werden hochgeladen..."); dialog.setCanceledOnTouchOutside(false); ConnectionTask.dialog.show(); } } protected Void doInBackground(String... args) { mUser = MainActivity.username; mPassword = MainActivity.password; Log.d(TAG,"Async: User= "+mUser+" Password= "+mPassword); Timer t = new Timer(); TimerTask tk = new TimerTask() { @Override public void run() { timeout = true; } }; t.schedule(tk, 100); if(timeout == true) { Log.d(TAG, "TimeOut = true"); onPostExecute(null); } try { authenticate(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } { try { sendData(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(temp!= null) { try { ReceiveMessageState(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Sendungfertig = true; if(MainActivity.asyncIsCanceled == true) { if (ConnectionTask.dialog.isShowing() == true) { ConnectionTask.dialog.dismiss(); } } isCancelled(); return null; } @Override protected void onPostExecute(Void v) { super.onPostExecute(v); Log.d(TAG, "PBar: Sollte enden"); if(MessageChecked==true) { if (ConnectionTask.dialog.isShowing() == true) { ConnectionTask.dialog.dismiss(); } if(isCancelled()==true) { if (ConnectionTask.dialog.isShowing() == true) { ConnectionTask.dialog.dismiss(); } MessageChecked = false; MainActivity.MessungStart = false; } } }
解决方案Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.
instead of doing
if(timeout == true) { Log.d(TAG, "TimeOut = true"); onPostExecute(null); // this should be removed
just
return null
there instead. This will return control toonPostExecute()
where you can close yourDialog
.Edit
I think you are making it too complicated with the
TimerTask
in this situation because everything will continue to run. What you can do is use awhile loop
and a counter for whatever time you want. So something likelong waitTime = 1000; // or whatever you want the timeout length to be long curWaitTime = 0; while (!timeout && curWaitTime < waitTime) { // put your network/file code here // if the data finishes then you can set timeout to true curWaitTime += 100; // or some other value to add Thread.sleep(100); } return null;
这篇关于如何超时的AsyncTask和解雇ProgressDialog?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!