Bonjour,
je cherche depuis quelque temps un exemple de code pour créer un bouton qui ouvre un socket et envoi un texte.
Ci après
mon main activity qui gère une interface avec des boutons fonctionnels:
ma class avec un socket fonctionnel
je n'arrive pas a lier les deux
****************************************************
****************************************
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116 package com.example.planar2; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button buttonWithActivityAsListener; private Button buttonWithInnerClassAsListener; private Button buttonWithInlineAsListener; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); buttonWithActivityAsListener = (Button) findViewById(R.id.button_with_activity_as_listener); buttonWithActivityAsListener.setOnClickListener(this); buttonWithInnerClassAsListener = (Button) findViewById(R.id.button_with_inner_class_as_listener); buttonWithInnerClassAsListener.setOnClickListener(new InnerOnClickButtonListener()); buttonWithInlineAsListener = (Button) findViewById(R.id.button_with_inline_class_as_listener); buttonWithInlineAsListener.setOnClickListener(new OnClickListener() { public void onClick(View v) { Toast.makeText(MainActivity.this, "Button with Inline Class as Listener has been clicked.", 2500).show(); } }); } /** You implement this method after adding OnClickListener interface to your Activity. **/ public void onClick(View v) { Toast.makeText(this, "Button with Activity as Listener has been clicked.", 2500).show(); } // /** Name of the method and it's triggering comes from main.xml file. * Button's widget attribute android:onClick allows you to specify name of the method * which you have to implement. **/ public void onButtonClick(View v) { Toast.makeText(this, "Button lancant le socket.", 2500).show(); } // /** Inner Class to respond to OnClick events. **/ class InnerOnClickButtonListener implements OnClickListener { public void onClick(View v) { Toast.makeText(MainActivity.this, "Button with Inner Class as Listener has been clicked.", 2500).show(); } } }
Socket
*****************************************
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66 package com.example.planar2; import java.io.*; import java.net.*; public class Client_Socket { public static void main(String[] args) throws IOException { Socket comm = null; InputStream is = null; try { comm = new Socket("192.168.1.11", 7072); comm.getOutputStream().write("image_delete -1 gui\n\n".getBytes()); is = comm.getInputStream(); int c; while((c = is.read()) != -1) System.out.write(c); } catch(Exception e) { System.err.println("Exception " + e.toString()); } finally { if (is!=null) is.close(); if (comm!=null) comm.close(); } } }
Partager