如何为登录活动执行异步任务?

时间:2013-09-06 10:59:02

标签: android android-asynctask

 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套接字发送登录消息的代码。我想为此活动执行异步任务。我怎么能这样做。

1 个答案:

答案 0 :(得分: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;
}