3

I have a background thread managing bluetooth data. In my first activity I have a handler, when some data is received a second activity is started:

   private Handler mHandler1 = new Handler() {
    @Override
    public void handleMessage(Message msg) {

        switch (msg.arg1) {

            // Conexión BT OK Inicia actividad de muestra de datos
            case 0:
                text.setText("Estado: Conectado");
                break;

            // Error en la conexión
            case 1:
                text.setText("Estado: Error de Conexión");
                break;

            // Recepción de Tramas
            case 2:
                byte[] mArray = new byte[128];
                mArray = (byte[]) msg.obj;

                if (mArray[0] == 0x55 && mArray[1] == (byte)0xAA)
                {
                    Intent intent = new Intent(BTSetupActivity.this, Monitor1BTActivity.class);
                    startActivity(intent);
                }
                break;

            // Recepción de Tramas fuera de rango bytes
            case 3:
                text.setText("Estado: Trama Recibida, Error Máx Data...");
                break;
        }
    }
};

After the intent to start the second activity I want to stop receiving messages from the thread in the first activity, I tried each following:

mHandler1.removeCallbacks(null);

then

mHandler1.removeCallbacksAndMessages(null);

and (my what value is 1).

mHandler1.removeMessages(1);

I tried these inside onStop(), onPause() and after the if statement on case 2

My problem is none of these worked. The handler remains alive and opening the second activity every time the condition is true. I tried setting a handler to the same thread on the second activity, that worked, but I think is not the right solution.

Madbyte
  • 95
  • 9
  • 1
    if you "want to stop receiving messages" then don't call `Handler.send[Empty]Message[Delayed]()`, `Handler.remove*` just removes pending messages from the queue – pskink Aug 28 '15 at 08:16
  • Did you mean I have to stop sending messages from the thread? I have several of these on the thread: `mHandler1.obtainMessage(1, x, y, z).sendToTarget();` – Madbyte Sep 01 '15 at 05:52

0 Answers0