onCancelled()在冰淇淋三明治中似乎有所不同(onCancelled() seems differnet in Ice Cream Sandwich)

编程入门 行业动态 更新时间:2024-10-25 02:29:38
onCancelled()在冰淇淋三明治中似乎有所不同(onCancelled() seems differnet in Ice Cream Sandwich)

只是想知道IceCream Sandwich发生了什么......

我有一个简单的AsyncTask从服务器读取数据。 单击Disconnect按钮时,则lServerTask.cancel(true); 被调用成功调用m_Running = false; onCancelled() ,设置m_Running = false; 从而打破了protected void doInBackground(Void...params)中的while循环。

使用我的Galaxy Nexus,我单击断开连接按钮,但任务不会退出。 并且仍然连接到服务器。 有没有人有任何想法?

非常感谢

标记

代码FYI -

// automatically done on worker thread (separate from UI thread) protected Void doInBackground(Void...params) { while(m_Running) { try { lRDS.readSocket(); } catch (IOException e) { lRDS.disconnectFromServer(); publishProgress(e.toString()); return null; } lDataBuffer = lRDS.getDataBuffer(); publishProgress(lDataBuffer); } return null; } @Override protected void onCancelled() { m_Running = false; connectRDSButton.setEnabled(true); disconnectRDSButton.setEnabled(false); }

从UI方面:

private class disconnectRDSButtonHandler implements View.OnClickListener { public void onClick(View v) { editRobotData.setText("Disconnect..."); if (lRobotDataServerTask.cancel(true) == true) { editRobotData.setText("Disconnected..."); } } }

Just wondered what's happened with IceCream Sandwich...

I had a simple AsyncTask reading data from a server. When the Disconnect button was clicked, then lServerTask.cancel(true); was called which was successfully calling onCancelled(), setting m_Running = false; thus breaking out of the while loop in protected void doInBackground(Void...params).

With my Galaxy Nexus, I click the disconnect button, but the task does not quit. And is still connected to the server. Does anyone have any ideas?

Many Thanks

Mark

Code FYI -

// automatically done on worker thread (separate from UI thread) protected Void doInBackground(Void...params) { while(m_Running) { try { lRDS.readSocket(); } catch (IOException e) { lRDS.disconnectFromServer(); publishProgress(e.toString()); return null; } lDataBuffer = lRDS.getDataBuffer(); publishProgress(lDataBuffer); } return null; } @Override protected void onCancelled() { m_Running = false; connectRDSButton.setEnabled(true); disconnectRDSButton.setEnabled(false); }

and from the UI side:

private class disconnectRDSButtonHandler implements View.OnClickListener { public void onClick(View v) { editRobotData.setText("Disconnect..."); if (lRobotDataServerTask.cancel(true) == true) { editRobotData.setText("Disconnected..."); } } }

最满意答案

好吧,好像我发现了这个问题。 我需要在doInBackground()方法的while循环中检查isCancelled(),然后从那里调用onCancelled()方法。 不知道为什么我没有把这看作是关于Android 2.2对HTC欲望的问题。

protected Void doInBackground(Void...params) { while(m_Running) { if (isCancelled () == true) { onCancelled(); }

希望这有助于某人。

标记

Okay it seems I've found the issue. I needed to check isCancelled() in the while loop of the doInBackground() method and then call the onCancelled() method from there. Not sure why I didn't see this as a problem on android 2.2 on HTC desire.

protected Void doInBackground(Void...params) { while(m_Running) { if (isCancelled () == true) { onCancelled(); }

Hope this helps someone.

Mark

更多推荐

本文发布于:2023-08-07 07:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1462377.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:明治   有所不同   冰淇淋   onCancelled   Sandwich

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!