问题
I want to send a plain or rich text from/to Android <-> Computer, probably to clipboard but not necessarily. How can I do that? I don't want to email or create a file in order write text into it.
Does the Android API allow to do that somehow? Maybe via NFC or Bluetooth?
I'd like it to be wireless.
回答1:
From your android device you can pass String to PC like this .
try {
Socket socket = new Socket(DESTINATION_ADDRESS, DESTINATION_PORT);
// Exmp : Socket socket = new Socket("192.168.0.101", 80);
OutputStream outToServer = socket.getOutputStream();
DataOutputStream out = new DataOutputStream(outToServer);
out.writeUTF(SEND_STRING);
// Exmp : out.writeUTF("TEST");
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
And from PC you have to receive the message and do action .
try {
InetAddress IP = InetAddress.getLocalHost();
ServerSocket serverSocket = new ServerSocket(1000);
} catch (UnknownHostException e) {
e.printStackTrace();
}
// Run an infinite loop to check if the message is received or not.
while (true) {
try {
Socket clientSocket = serverSocket.accept();
DataInputStream in = new DataInputStream(new BufferedInputStream(clientSocket.getInputStream()));
String message = in.readUTF(); // Get the message which send from android device.
/* do other staff after getting the message . */
}atch (Exception ex) {
e.printStackTrace();
}
}
来源:https://stackoverflow.com/questions/36640944/how-to-send-receive-text-between-android-and-computer