1 | package de.test.hello;
|
2 |
|
3 | import java.io.BufferedReader;
|
4 | import java.io.BufferedWriter;
|
5 | import java.io.DataOutputStream;
|
6 | import java.io.IOException;
|
7 | import java.io.InputStreamReader;
|
8 | import java.io.OutputStreamWriter;
|
9 | import java.io.PrintWriter;
|
10 | import java.net.Socket;
|
11 | import java.net.UnknownHostException;
|
12 |
|
13 | import android.app.Activity;
|
14 | import android.app.AlertDialog;
|
15 | import android.os.Bundle;
|
16 | import android.view.View;
|
17 | import android.view.View.OnClickListener;
|
18 | import android.widget.Button;
|
19 | import android.widget.EditText;
|
20 |
|
21 |
|
22 | public class HelloWorkshopActivity extends Activity implements OnClickListener, Runnable {
|
23 | /** Called when the activity is first created. */
|
24 |
|
25 |
|
26 | private Button ConnectButton;
|
27 | private Button SendButton;
|
28 | private Button DisconnectButton;
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | public void onCreate(Bundle savedInstanceState) {
|
34 | super.onCreate(savedInstanceState);
|
35 | setContentView(R.layout.main);
|
36 | ConnectButton = (Button)findViewById(R.id.Connect);
|
37 | ConnectButton.setOnClickListener(this);
|
38 | SendButton = (Button)findViewById(R.id.Send);
|
39 | SendButton.setOnClickListener(this);
|
40 | DisconnectButton = (Button)findViewById(R.id.Disconnect);
|
41 |
|
42 | DisconnectButton.setOnClickListener(this);
|
43 |
|
44 | }
|
45 |
|
46 | @Override
|
47 | public void onClick(View v) {
|
48 | EditText nameField = (EditText) findViewById(R.id.name_field);
|
49 | String name = nameField.getText().toString();
|
50 | if (name.length() == 0) {
|
51 | new AlertDialog.Builder(this)
|
52 | .setMessage(R.string.error_name_missing)
|
53 | .setNeutralButton(R.string.error_ok, null)
|
54 | .show();
|
55 | return;
|
56 | }
|
57 | if (v == ConnectButton){
|
58 | try {
|
59 | connectTOTCP();
|
60 | }
|
61 | catch (Exception e ){
|
62 | new AlertDialog.Builder(this)
|
63 | .setMessage("FEHLER")
|
64 | .setNeutralButton(R.string.error_ok, null)
|
65 | .show();
|
66 | }
|
67 | }
|
68 | if (v == SendButton){
|
69 | try {
|
70 | out.write(name);
|
71 | out.flush();
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | String message = in.readLine();
|
77 |
|
78 |
|
79 |
|
80 | nameField.setText(message);
|
81 |
|
82 |
|
83 | }
|
84 | catch (IOException e) {
|
85 | // TODO Auto-generated catch block
|
86 | //e.printStackTrace();
|
87 | }
|
88 | }
|
89 | if (v == DisconnectButton){
|
90 | try {
|
91 | out.write("/DISCONNECT");
|
92 | out.flush();
|
93 | } catch (IOException e) {
|
94 | // TODO Auto-generated catch block
|
95 | //e.printStackTrace();
|
96 | }
|
97 | }
|
98 | }
|
99 |
|
100 | public Socket clientSocket;
|
101 | public BufferedReader in;
|
102 | public BufferedWriter out;
|
103 |
|
104 |
|
105 |
|
106 |
|
107 | public void connectTOTCP() {
|
108 |
|
109 |
|
110 | try {
|
111 | clientSocket = new Socket("192.168.1.2", 1522);
|
112 | in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
|
113 | out = new BufferedWriter(new OutputStreamWriter(clientSocket.getOutputStream()));
|
114 | out.write("/CONNECT|CLIENTAndroid");
|
115 | out.flush();
|
116 |
|
117 |
|
118 |
|
119 | } catch (UnknownHostException e) {
|
120 | new AlertDialog.Builder(this)
|
121 | .setMessage("FEHLER")
|
122 | .setNeutralButton(R.string.error_ok, null)
|
123 | .show();
|
124 | } catch (IOException e) {
|
125 | new AlertDialog.Builder(this)
|
126 | .setMessage("FEHLER")
|
127 | .setNeutralButton(R.string.error_ok, null)
|
128 | .show();
|
129 | }
|
130 |
|
131 | }
|
132 |
|
133 | @Override
|
134 | public void run() {
|
135 | // TODO Auto-generated method stub
|
136 |
|
137 | }
|
138 |
|
139 |
|
140 | }
|