在我的 android 项目中,我已经成功地将 2 个字符串 [(TV ON(by button 1) and TV OFF(by button 2)] 从我的 android 手机通过 2 个简单的按钮使用 WiFi 传递到我的 PC。但在这里我需要传递 2使用单个切换按钮 [(TV ON(单击切换按钮)和 TV OFF(再次单击切换按钮)] 而不是 2 个简单按钮(上面提到)的字符串。对不起我的英语不好。感谢 adv.java 代码-
package com.example.wifitoggle;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import android.widget.ToggleButton;
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 ToggleButton toggleButton1;
private String messsage;
int port = 0; //
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// etIP = (EditText) findViewById(R.id.editText1);
// etPort = (EditText) findViewById(R.id.editText2);
//etMsg = (EditText) findViewById(R.id.editText3);
button = (Button) findViewById(R.id.button1);
button1 = (Button) findViewById(R.id.button2);
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
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();
}
});
}
}