I am successfully passing string from my android to computer(through WiFi) using two simple button.but my question is i want to use single radio button (as toggle) instead of two simple button .here is my code
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Socket client;
private PrintWriter printwriter;
private Button button;
private Button button1;
private String messsage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
messsage = "TV ON" ; //etMsg.getText().toString();
//etMsg.setText("");
// port = Integer.parseInt(etPort.getText().toString());
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
try
{
// client = new Socket(etIP.getText().toString(), port);
client = new Socket("1.2.3.4",2000);
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(messsage);
printwriter.flush();
printwriter.close();
client.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
messsage = "TV OFF" ; //etMsg.getText().toString();
//etMsg.setText("");
// port = Integer.parseInt(etPort.getText().toString());
new Thread(new Runnable()
{
@Override
public void run() {
// TODO Auto-generated method stub
try
{
// client = new Socket(etIP.getText().toString(), port);
client = new Socket("1.2.3.4",2000);
printwriter = new PrintWriter(client.getOutputStream(),true);
printwriter.write(messsage);
printwriter.flush();
printwriter.close();
client.close();
}
catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
});
}
}