Buongiorno ragazzi. Ho un altro piccolo problemino.
Ho il mio modestissimo client, che si collega ad un server. Nella richiesta di connessione a quest'ultimo o alla creazione del socket e dei relativi buffer, vorrei che partisse un timer (non visibile) per evitare che mi si congeli la schermata e mi si blocchi l'emulatore. Ho pensato questa soluzione:
1public int connetti(String ip , int port) {
2
3temp = new String[1];
4temp[0] = new String();
5
6timer = new CountDownTimer(5000,5000) {
7
8
9public void onTick(long millisUntilFinished) {
10// TODO Auto-generated method stub
11//non fa niente
12}
13
14
15public void onFinish() {
16// TODO Auto-generated method stub
17temp[0] = "KO";
18}
19
20}.start();
21
22try{
23ipAddress = InetAddress.getByName(ip);
24}catch(UnknownHostException e){
25e.printStackTrace();
26timer.cancel();
27return 1;
28}
29
30try{
31
32socket = new NewSocket(ipAddress,port);
33
34}catch(IOException e) {
35e.printStackTrace();
36timer.cancel();
37return 2;
38}
39
40try {
41
42reader = new NewBufferedReader(new InputStreamReader(socket.getInputStream()));
43
44writer = new NewPrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
45
46} catch (IOException e) {
47e.printStackTrace();
48timer.cancel();
49return 3;
50}
51
52
53if(temp[0].equalsIgnoreCase("KO")) return 5;
54timer.cancel();
55
56return 0;
57
58}
Il valore di ritorno (int) mi serve soltanto per gestire poi il tipo di errore e regolarmi di conseguenza..
In questo modo pero non riesco cmq a far ritornare dal metodo un valore..
Qualcuno può darmi una mano? Grazie mille.![]()