0
 public static boolean SendMessage(final String response) 
{

    OutputStream out;
    try {
        out = socket.getOutputStream();
        writeResponse(out,response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        mRun=true;
        return false;
    }
    return true;

这是我将登录消息发送到 tcp 套接字的代码。我想为此活动执行异步任务。我怎样才能做到这一点。

4

1 回答 1

0

尝试这样的代码并new MyAsync().execute();在您想发送短信时执行

public class MyAsync extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected Boolean doInBackground(Void... params) {

        return SendMessage(response);
    }

}

public static boolean SendMessage(final String response) {

    OutputStream out;
    try {
        out = socket.getOutputStream();
        writeResponse(out, response);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        mRun = true;
        return false;
    }
    return true;
}
于 2013-09-06T11:06:33.143 回答