Bonjour.
Voila mon problème :
J'ai dans mon application plusieurs Activity qui doivent toutes utiliser un thread que je crée dans la première.
Pour ce faire, j'ai crée un Service qui est lancé par la première Activity et qui lance mon Thread.
Le soucis c'est que je n'arrive pas a envoyer des informations au service en question. (Appeler mes methodes setParams() et sendMessage())
Si quelqu'un peut m'aider, d'avance merci.
Mon code qui crée le service :
Ma classe Service :
Code : Sélectionner tout - Visualiser dans une fenêtre à part startService(new Intent(this, SocketService.class));
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 public class SocketService extends Service { public class SocketBinder extends Binder { SocketService getService() { return SocketService.this; } } private ThreadEnvoi thE; public IBinder onBind(Intent intent) { return new SocketBinder(); } public void onCreate() { super.onCreate(); Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show(); } public void onStart(Intent intent, int startId) { Toast.makeText(this, "Service started", Toast.LENGTH_LONG).show(); super.onStart(intent, startId); } public void onDestroy() { thE.shutdown(); super.onDestroy(); } public void setParams(PrintWriter pw) { thE = new ThreadEnvoi(pw); Thread envoi = new Thread(thE); envoi.start(); } public void sendMessage(String m) { thE.sendMessage(m); } }
Partager