Ad

This Handler Could Be Static Or Leaks Might Occur For Bluetooth Handler

- 1 answer

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.bluetooth_datadisplay);


        txtString = (TextView) findViewById(R.id.txtString);
        txtStringLength = (TextView) findViewById(R.id.testView1);
        sensorView0 = (TextView) findViewById(R.id.sensorView0);
        sensorView1 = (TextView) findViewById(R.id.sensorView1);
        sensorView2 = (TextView) findViewById(R.id.sensorView2);
        sensorView3 = (TextView) findViewById(R.id.sensorView3);

     bluetoothIn = new Handler() {
        public void handleMessage(android.os.Message msg) {
            if (msg.what == handlerState) {
                String readMessage = (String) msg.obj;
                recDataString.append(readMessage);
                int endOfLineIndex = recDataString.indexOf("~");
                if (endOfLineIndex > 0) {
                    String dataInPrint = recDataString.substring(0, endOfLineIndex);
                    txtString.setText("Data Received = " + dataInPrint);
                    int dataLength = dataInPrint.length();
                    txtStringLength.setText("String Length = " + String.valueOf(dataLength));

                    if (recDataString.charAt(0) == '#')
                    {
                        String sensor0 = recDataString.substring(1, 5);
                        String sensor1 = recDataString.substring(6, 10);
                        String sensor2 = recDataString.substring(11, 15);
                        String sensor3 = recDataString.substring(16, 20);

                        sensorView0.setText(" Sensor 0 Voltage = " + sensor0 + "V");
                        sensorView1.setText(" Sensor 1 Voltage = " + sensor1 + "V");
                        sensorView2.setText(" Sensor 2 Voltage = " + sensor2 + "V");
                        sensorView3.setText(" Sensor 3 Voltage = " + sensor3 + "V");
                    }
                    recDataString.delete(0, recDataString.length());

                }
            }
        }
    };
  }

Hi I getting this warning of handler could be static or leaks might occur. I had look at other post but it doesn't help me to solve my problem. :( The warning happened at the start of bluetoothIn to the end.

rederror after moving out of on createhandler is in grey

Ad

Answer

possible duplicate this question. So the solution is below:

question. So the solution is below:

    static class BluetoothInHandler extends Handler {
            private final WeakReference<YourActivityName> mActivity; 

            IncomingHandler(YourActivityName activity) {
                mActivity= new WeakReference<YourActivityName>(activity);
            }
            @Override
            public void handleMessage(Message msg)
            {
                final YourActivityName thizz = mActivity.get();
                if (thizz == null) {
                    return;
                 }
                if (msg.what == thizz.handlerState) {
                  // Do what you need
                 }
            }
    }
Ad
source: stackoverflow.com
Ad