cancelConnect()在AsyncTask中不起作用

时间:2014-05-06 11:11:42

标签: android android-asynctask

我需要在通过WiFi Direct发送文件后关闭连接。我将管理器和通道传递给AsyncTask(它处理crating Socket)并在doInBackground()的末尾调用cancelConnect()。它无法关闭连接,但是当我在MainActivity中调用cancelConnect()时,它可以正常工作。为什么会这样?

package com.ex.bb;


public class SocketAsync extends AsyncTask<Wrapper,Object,Object> {

private Context context;
 //   private TextView statusText;

    public SocketAsync(Context context) {
        this.context = context;

    }

public static void main(String[] args) {
    // TODO Auto-generated method stub

}
protected String doInBackground(Wrapper...w) {

    //File Sending Code
     w[0].manager.cancelConnect(w[0].channel,new ActionListener(){
        @Override 
        public void onSuccess() { 
            Log.i(MainActivity.TAG, "Disconnected from device");
        } 

        @Override 
        public void onFailure(int reason) {
            Log.i(MainActivity.TAG, "Couldn't disconnect from device");
        } 
    }); 


}

Wrapper类由经理和渠道组成。

1 个答案:

答案 0 :(得分:0)

不要在doInBackground中执行cancelConnect(),而是在onPostExecute()中执行