Bonjour,

Voila j'utilise mon pc pour envoyer une liste de numéro de tel + un msg. (msg;tel1;tel2;etc)
J'utilise le code suivant dans mon appli Android pour recevoir la liste et envoyer les messages.
La réception de la liste se passe bien mais lors de l'envoi l'appli s'arrête.
Je pense que je ne mets pas la boucle sur SendSMS au bon endroit.
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import java.io.IOException;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
 
public class Web2Sms extends Activity
{
TextView info;
String Qui;
EditText ipinfo;
TextView t;
private List<String> listphone = new ArrayList<String>();     
 
      class SocketListener implements Runnable
 
      {
 
            String str;
            String str1;
            String numero;
            String msg;
            String _list[];
            public void run()
 
            {
 
                  DatagramSocket socket;
 
                  DatagramPacket packet;
 
                  byte[] buf = new byte[256];
 
                  try
 
                  {
 
                        socket = new DatagramSocket (8080);
 
 
                        while (true)
 
                        {
 
 
 
 
                              packet = new DatagramPacket (buf, buf.length);
                              socket.receive (packet);
 
                              System.out.println ("Received packet");
 
                              String s = new String (packet.getData());
 
 
 
                              CharSequence cs = t.getText ();
 
                              str1 = s;
                              ecrire("Réception données: ");
                              _list = str1.split(";");
                              for(int i =0;i<_list.length;i++){
                                  listphone.add(_list[i]);
 
                              }
 
                        }
 
                  }
 
                  catch (IOException e)
 
                  {
 
                        Log.e(getClass().getName(), e.getMessage());
 
                  }
                       if(!listphone.isEmpty()) {
                         for (int u = 1;u<listphone.size();u++){
                             sendSMS(listphone.get(u), listphone.get(0));
                             ecrire("msg au " + listphone.get(u) + " parti" );
                         }
                     }
            }
 
      }
      private void ecrire(final String txt){
          t = (TextView)findViewById(R.id.textView1); 
                      t.post(new Runnable()
 
                              {
 
                                    public void run()
 
                                    {
                                 try{                                      
                                t.append(txt);
                                   }catch(Exception e) {
                                        System.out.println(e.toString());
                                    }    
 
                                    }
 
                              }
 
                                          );
 
      }
        public void ok(String var){
            String s = var;
 
     try
 
                        {
 
                              final DatagramSocket socket = new DatagramSocket ();
 
 
 
                              byte[] buf = new byte[256];
 
 
 
                              buf = s.getBytes ();
 
                              InetAddress address = InetAddress.getByName (ipinfo.getText().toString());
 
                              final DatagramPacket packet = new DatagramPacket (buf, buf.length, address, 8080);
 
 
 
                              new Thread ()
 
                              {
 
                                    public void run ()
 
                                    {
 
                                          try
 
                                          {
 
 
 
                                                socket.send (packet);
 
 
 
                                          }
 
                                          catch (IOException e1)
 
                                          {
 
                                                // TODO Auto-generated catch block
 
                                                e1.printStackTrace();
 
                                          }
 
                                          socket.close();
 
                                    }
 
                              }.start ();
 
                        }
 
                        catch (SocketException e1) {}
 
                        catch (UnknownHostException e2) {}
        }
 
 
      @Override
 
      protected void onCreate(Bundle savedInstanceState)
 
      {
 
            super.onCreate(savedInstanceState);
 
            setContentView(R.layout.main);
 
            info = (TextView) findViewById(R.id.info);
            info.setText("Mon adresse : " + getIpAddress());
            ipinfo = (EditText) findViewById(R.id.editText1);
            ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
 
            if (!mWifi.isConnected()) {
             Toast.makeText(getApplicationContext(), "Démarrer votre wifi",
		Toast.LENGTH_LONG).show();       
                }
 
 
 
            Thread t = new Thread (new SocketListener ());
 
            t.start();
 
      }
 
 
 
      @Override
 
      public boolean onCreateOptionsMenu(Menu menu) {
 
 
            return true;
 
      }
 private String getIpAddress() {
  String ip = "";
  try {
   Enumeration<NetworkInterface> enumNetworkInterfaces = NetworkInterface
     .getNetworkInterfaces();
   while (enumNetworkInterfaces.hasMoreElements()) {
    NetworkInterface networkInterface = enumNetworkInterfaces
      .nextElement();
    Enumeration<InetAddress> enumInetAddress = networkInterface
      .getInetAddresses();
    while (enumInetAddress.hasMoreElements()) {
     InetAddress inetAddress = enumInetAddress.nextElement();
 
     if (inetAddress.isSiteLocalAddress()) {
      ip +=  inetAddress.getHostAddress() + "\n";
     }
 
    }
 
   }
 
  } catch (SocketException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   ip += "Something Wrong! " + e.toString() + "\n";
  }
 
  return ip;
 }
     //---sends an SMS message to another device---
    private void sendSMS(String phoneNumber, String message)
    {        
        String SENT = "SMS_SENT";
        String DELIVERED = "SMS_DELIVERED";
 
        PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
            new Intent(SENT), 0);
 
        PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,
            new Intent(DELIVERED), 0);
 
        //---when the SMS has been sent---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS Envoyé", 
                                Toast.LENGTH_SHORT).show();
                       // ok("Ok");
                        break;
                    case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
                        Toast.makeText(getBaseContext(), "Erreur", 
                                Toast.LENGTH_SHORT).show();
                        ok("Ko");
                        break;
                    case SmsManager.RESULT_ERROR_NO_SERVICE:
                        Toast.makeText(getBaseContext(), "No service", 
                                Toast.LENGTH_SHORT).show();
                        //ok("Ko");
                        break;
                    case SmsManager.RESULT_ERROR_NULL_PDU:
                        Toast.makeText(getBaseContext(), "Null PDU", 
                                Toast.LENGTH_SHORT).show();
                       // ok("Ko");
                        break;
                    case SmsManager.RESULT_ERROR_RADIO_OFF:
                        Toast.makeText(getBaseContext(), "Radio off", 
                                Toast.LENGTH_SHORT).show();
                       // ok("Ko");
                        break;
                }
            }
        }, new IntentFilter(SENT));
 
        //---when the SMS has been delivered---
        registerReceiver(new BroadcastReceiver(){
            @Override
            public void onReceive(Context arg0, Intent arg1) {
                switch (getResultCode())
                {
                    case Activity.RESULT_OK:
                        Toast.makeText(getBaseContext(), "SMS delivered", 
                                Toast.LENGTH_SHORT).show();
                        ok("Ok");
                        break;
                    case Activity.RESULT_CANCELED:
                        Toast.makeText(getBaseContext(), "SMS not delivered", 
                                Toast.LENGTH_SHORT).show();
                        ok("Ko");
                        break;                        
                }
            }
        }, new IntentFilter(DELIVERED));        
 
        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);        
    }
}