2010-10-26 19 views
9

Ho un servizio che monitora una connessione socket. Quando la connessione viene persa, è necessario visualizzare un Toast che informa l'utente che si sta ricollegando. Questo funziona bene la prima volta. Dopodiché vedo il file di accodamento nel registro ma il brindisi non viene visualizzato. Qualsiasi idea è apprezzata. Ho pensato che sarebbe stata una cosa facile da aggiungere, ma mi manca qualcosa.Android Toast avviato dal servizio viene visualizzato solo una volta

voce del registro

INFO/NotificationService (118): enqueueToast pkg = com.abc [email protected] durata = 1

Codice che chiama la Toast

public class ConnectionService extends Service 
{ ..... 

public void restartConnection() 
{ 
    try 
    { 
    Log.i(this.toString(), "Attempting to reconnect..."); 

    // increase the wait between each retry until the max is reached 
    int sleepTime = reconnectCounter * MIN_RECON_WAIT; 

    if (sleepTime > MAX_RECON_WAIT) 
    { 
     sleepTime = MAX_RECON_WAIT; 
    } 

    String msg = "The connection has been lost. Restart attempt will start in: " + sleepTime/1000 + " seconds"; 

    Log.i(this.toString(), msg); 
    Toast.makeText(getApplicationContext(), msg , Toast.LENGTH_LONG).show(); 

    Thread.sleep(sleepTime); 

    // increment the counter 
    reconnectCounter++; 

    this.startConnectionThread(); 

    } 
    catch (Exception e) 
    { 
     Log.e(this.toString(), "Exception: " + e.toString()); 
     e.printStackTrace(); 
    } 
}// end retartConnection 
+0

È probabilmente un problema di threading. Stai chiamando Toast.show() dal thread dell'interfaccia utente o da uno separato? Potresti dare un po 'più di contesto a questo metodo. – Vuk

+0

Chiamato dall'interno di una classe del servizio che è stata avviata da una chiamata bindService dall'attività che viene prima visualizzata all'utente. Speravo di usare una chiamata runOnUiThread per mostrare il brindisi, ma non riuscivo a capire come usarlo nel servizio. – bursk

risposta

12

Sì, si potrebbe andare con la runOnUiThread, che è un modo legittimo.
Inoltre, puoi provare l'alternativa dell'handler. In ogni caso dovrebbe funzionare.

Ecco un codice dalla parte superiore della mia testa. Ora non ho l'SDK per testarlo, ma penso che dovrebbe darti un'idea generale.

public class ConnectionService extends Service { 
    private Handler handler = new Handler(); 

    public void restartConnection(){ 
    int sleepTime = reconnectCounter * MIN_RECON_WAIT; 
    if (sleepTime > MAX_RECON_WAIT) 
    { 
     sleepTime = MAX_RECON_WAIT; 
    } 
    String msg = "The connection has been lost. Restart attempt will start in: " + sleepTime/1000 + " seconds"; 
    (new Timer()).schedule(
    new TimerTask() { 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
       Toast.makeText(getApplicationContext(), "msg", Toast.LENGTH_LONG).show(); 
       reconnectCounter++; 
       this.startConnectionThread() 
       } 
      }); 
     } 
    }, sleepTime); 
    }//end restartConnection 

}//end ConnectionService 
+0

Eeeek !!! Proprio quello di cui avevo bisogno, grazie mille! – bursk

+1

non è una buona soluzione. Migliore e più semplice: http://www.jjoe64.com/2011/09/show-toast-notification-from-service.html – appsthatmatter

+0

Grazie per l'utile post! – Warwicky

Problemi correlati