2011-05-26 7 views
12

Android: ¿Cómo puedo mostrar un brindis de un hilo que se ejecuta en un servicio remoto? Cada vez que corro de un hilo diferente en un servicio, la aplicación falla ...Android: ¿Cómo puedo mostrar un brindis de un hilo que se ejecuta en un servicio remoto?

public class BroadcastService extends Service { 

private static final String TAG = BroadcastService.class.getSimpleName(); 
private MessageFormatter mFormatter = new MessageFormatter(); 
private BroadcastComm broadCastComm = new BroadcastComm(); 
private Task commTask = new Task(); 
private volatile static boolean stopBroadcastRequested = false; 
private volatile static boolean isSocketOpen = false; 
private volatile static byte[] messageToBeSent; 
private Handler serviceHandler; 
private ConnectivityStatus connStatus; 


@Override 
public void onCreate() { 
    super.onCreate(); 
    Log.e(TAG, "Service creating"); 
} 

@Override 
public void onStart(Intent intent, int startId) { 
    super.onStart(intent, startId); 
    serviceHandler = new Handler(); 
    serviceHandler.post(commTask); 
    stopBroadcastRequested = false; 
    messageToBeSent = mFormatter.formBroadCastMessage("GET_PERIPH_DATA"); 
} 


class Task implements Runnable{ 

    @Override 
    public void run() { 
     while(!stopBroadcastRequested){ 
          Toast.makeText(context, "SSSSSSSSSS", 5).show(); 
      Log.i(TAG, "Thread Task started");   
      try { 
       Log.d("SERVICE CLASS", "STARTED THREAD - Writing in output stream"); 
       isSocketOpen = broadCastComm.isAliveOrOpenSocket("192.168.43.2", 6000, 17, 0); 

       if(isSocketOpen){ 
        OutputStream outStream = broadCastComm.getCurrentOutputStream(); 

        outStream.write(messageToBeSent); 
        if(Integer.valueOf(messageToBeSent[2]) != (byte)0xA0){ 
         Log.e("REVERTING", "REVERTING"); 
         messageToBeSent = mFormatter.formBroadCastMessage("GET_PERIPH_DATA"); 
        } 

        Log.d("OUTPUT STREAM", "Message sent ->" + ByteArrayToString(messageToBeSent)); 
       } 

       Thread.sleep(3000L); 
       if(isSocketOpen){ 
        Log.d("SERVICE CLASS", "Started input"); 
        InputStream inStream = broadCastComm.getCurrentInputStream(); 
        BufferedInputStream buf = new BufferedInputStream(inStream); 
        byte[] buffer; 

        while(buf.available() > 0){ 
         Log.d("SERVICE CLASS", "Input available"); 
         int size = buf.available(); 
         buffer = new byte[size]; 
         inStream.read(buffer, 0, size); 
         if(buffer[2] == (byte)0xA0){ 
          BroadcastPacket packet = broadCastComm.decodeSocketData(buffer); 
          synchronized (incomingPacketLock) { 
           latestBroadcastPacket = packet; 
          } 
          synchronized (listeners) { 
           for (BroadcastListener listener : listeners) { 
            try { 
             listener.handlePacketsUpdated(); 
            } catch (RemoteException e) { 
             Log.w(TAG, "Failed to notify listener " + listener, e); 
            } 
           } 
          } 
         } 
        } 
       } 

      } catch (Throwable t) { 
       Log.e(TAG, "Failed to retrieve data in thread", t); 
      } 
      Log.d("SERVICE CLASS", "End of THREAD"); 

     } 
     if(stopBroadcastRequested){ 
      Log.e("SERVICE", "STOPPED THREAD"); 
     } 
    } 

    public synchronized void stopThread(){ 
     stopBroadcastRequested = true; 
    } 

} 

    ........... 

@Override 
public IBinder onBind(Intent intent) { 
    if (BroadcastService.class.getName().equals(intent.getAction())) { 
     Log.d(TAG, "Bound by intent " + intent); 
     return apiEndpoint; 
    } else { 
     return null; 
    } 
} 


    ......... 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    Log.e(TAG, "Service destroying"); 
    stopBroadcastRequested = true; 
    broadCastComm.clearConnections(); 
    try { 
     serviceHandler.removeCallbacks(commTask); 
     serviceHandler = null; 

    } catch (Exception e) { 
     Log.d("SERVICE", "FAILED TO REMOVE CALL BACKS"); 
    } 

    commTask.stopThread(); 
    //currentThread = null; 
} 

}

+0

¿Muestra el seguimiento de la pila? – Femi

+0

java.lang.RuntimeException: No se puede crear el controlador en el hilo que no ha llamado a Looper.prepare() – Abraham

+0

¿Alguna idea de cómo puedo hacer esto? – Abraham

Respuesta

44

Esta es la forma en que lo hice. Por supuesto, debes pasar el contexto apropiado.

Handler h = new Handler(context.getMainLooper()); 

    h.post(new Runnable() { 
     @Override 
     public void run() { 
      Toast.makeText(context,message,Toast.LENGTH_LONG).show(); 
     } 
    }); 
+0

Nifty. ¡No sabía que podrías hacer esto! – Femi

+0

Gracias ... funcionó ... :) – Abraham

+12

¿Qué es 'context' aquí? – emaillenin

0

no estoy seguro, pero esto se ve de nuevo como intentar invocar métodos de interfaz gráfica de usuario en las discusiones no gráficos.

2

si alguien no entiende qué contexto hay en @Alex Gitelman answer. escriba su nombre de clase en lugar del contexto en el que está escribiendo este código de controlador como este

Handler h = new Handler(MyClassName.this.getMainLooper()); 

h.post(new Runnable() { 
    @Override 
    public void run() { 
     Toast.makeText(MyClassName.this,"show toast message",Toast.LENGTH_LONG).show(); 
    } 
}); 
Cuestiones relacionadas