如何在android中的asynctask中的doInBackground中获取click事件

时间:2015-08-15 09:22:38

标签: android android-asynctask

我试图从套接字编程中的编辑文本中读取。我在后台做了一个,它创建了一个套接字并从中读取。单击按钮时,我想从中连续阅读。我的程序如下:

 public class MainActivity extends AppCompatActivity {
    private static final String TAG = "CLIENT_MESSAGE";
    EditText ip_address;
    EditText port_number;
    EditText message_client;
    Button button_send;
    Button button_cancel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    public void connect(View view) {
//        ip_address = (EditText) findViewById(R.id.ip_address);
//        ip_address.setText("192.168.9.100");
//        port_number = (EditText) findViewById(R.id.port_number);
//        port_number.setText("8080");
        message_client = (EditText) findViewById(R.id.message_client);
        button_send = (Button)findViewById(R.id.button_send);
        button_cancel = (Button)findViewById(R.id.button_cancel);
        Log.d(TAG, "connecting to the server.");
//        new ConnectToServer(ip_address.getText().toString(), port_number.getText().toString(), message_client,button_send,button_cancel).execute();
        new ConnectToServer(this,"192.168.9.100","8080", message_client,button_send,button_cancel).execute();

    }
}

class ConnectToServer extends AsyncTask<Void, DataOutputStream, Void> {
    private static final String TAG = "CLIENT_MESSAGE";
    String ip_address;
    int port_number;
    EditText message_client;
    Button button_send;
    Button button_cancel;
    boolean send = false;
    boolean cancel = false;
    Activity activity;

    public ConnectToServer(Activity activity,String ip_address, String port_number, EditText message_client,Button button_send,Button button_cancel) {
        this.ip_address = ip_address;
        this.port_number = Integer.parseInt(port_number);
        this.message_client = message_client;
        this.button_cancel = button_cancel;
        this.button_send = button_send;
        this.activity = activity;
        ;
    }



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

        try {
            Socket socket = new Socket(ip_address, port_number);

            if (LoggerConfig.TAG) {
                Log.d(TAG, "the socket is created at " + ip_address);
            }

            final DataOutputStream output = new DataOutputStream(socket.getOutputStream());

            publishProgress(output);

            socket.close();
        } catch (IOException e) {
            if (LoggerConfig.TAG) {
                Log.d(TAG, "Could not connect.");
            }
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onProgressUpdate(DataOutputStream... values) {
        super.onProgressUpdate(values);
        while (true )
        {
            //how would I get the on click events here?
        }

    }
}

我必须按钮,发送和取消。单击“发送”按钮时,如何将数据发送到服务器?并且,在单击取消时关闭套接字?

1 个答案:

答案 0 :(得分:0)

在UI线程上调用

onProgressUpdate 以在其上发布更新。从您的问题我所理解的是您想要点击按钮事件...为什么不使用 button.setOnClickListener(new OnClickListener(){/ * Some Code * /}}; 如果我错了,请告诉我。

如果情况并非如此,那么您应该创建自己的监听器并将其附加到asyctask和activity ...让我知道如果你想这样,我会添加片段