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
| public class MainActivity extends Activity implements View.OnTouchListener
{
private Socket socket;
private BufferedReader inFromServer;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b =(Button) findViewById(R.id.bouton);
b.setOnTouchListener(this);
}
@Override
public void onStart(){
super.onStart();
String numéro,message;
String[] donnée;
Toast toast;
try{
message=inFromServer.readLine();
donnée=message.split("#");
numéro=donnée[0];
message=donnée[1];
toast=Toast.makeText(getApplicationContext(),"Message :"+message, Toast.LENGTH_LONG);
toast.show();
SmsManager sms;
sms=SmsManager.getDefault();
sms.sendTextMessage(numéro, null, message, null, null);
toast=Toast.makeText(getApplicationContext(),"Message envoyé à "+numéro, Toast.LENGTH_LONG);
toast.show();
}
catch(Exception e){
toast=Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG);
toast.show();
}
}
public boolean onTouch(View view, MotionEvent event){
Toast toast;
int i=0;
try{
EditText text=(EditText) findViewById(R.id.adIp);
if(socket!=null){
socket.close();
inFromServer.close();
}
socket=new Socket(text.getText().toString(),16000);
inFromServer = new BufferedReader(new InputStreamReader(socket.getInputStream()));
}
catch(Exception e){
toast=Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG);
toast.show();
}
return true;
}
public void onDestroy(){
try{
socket.close();
inFromServer.close();
}
catch(Exception e){
}
}
} |
Partager