只是想知道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
更多推荐
发布评论