线程不会在启动另一个活动两次时更新textview

时间:2015-07-09 22:09:55

标签: java android multithreading bluetooth

我有一个通过蓝牙与Arduino连接的应用程序。该应用程序还可以,但是当我开始另一项活动时,我的textview不会更新。 我有一个Thread读取蓝牙数据,我有一个刷新textview的计时器。

如果您是第一次启动活动并且我返回主活动textview刷新正常,但如果我在返回主时再次启动活动,则textview不会刷新。

HELP !!!

OnCreate中:

bluetoothIn = new Handler() {
    public void handleMessage(android.os.Message msg) {
        if (msg.what == handlerState) {
            String readMessage = (String) msg.obj;
            MetrosRecorridos += ((Calibracion / Imanes / 1000) * readMessage.length()) * Sentido;
        }
    }
};

在与蓝牙连接的按钮中:

mConnectedThread = new ConnectedThread(mmSocket);
mConnectedThread.start();

Thread

private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    //creation of the connect thread
    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;
        try {
            //Create I/O streams for connection
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[256];
        int bytes;

        // Keep looping to listen for received messages
        while (true) {
            try {
                bytes = mmInStream.read(buffer);            //read bytes from input buffer
                String readMessage = new String(buffer, 0, bytes);
                // Send the obtained bytes to the UI Activity via handler
                bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
            } catch (IOException e) {
                txtConectado = "Sonda: Desconectado";
                //Toast.makeText(getBaseContext(), "Fallo de conexión", Toast.LENGTH_LONG).show();
                break;
            }
        }
    }
    //write method
    public void write(String input) {
        byte[] msgBuffer = input.getBytes();           //converts entered String into bytes
        try {
            mmOutStream.write(msgBuffer);                //write bytes over BT connection via outstream
        } catch (IOException e) {
            //if you cannot write, close the application
            Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
            finish();

        }
    }
}

刷新Timer

textview
public void startTimer(){
    t = new Timer();
    task = new TimerTask() {
        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    TextView t;
                    t=(TextView)findViewById(R.id.txtA);
                    t.setText(""+MetrosRecorridos);
                }
            });
        }
    };
    t.scheduleAtFixedRate(task, 0, 10);
}

当我调用另一个活动时的代码:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.opc_ajustes) {
        bluetoothIn.removeCallbacksAndMessages(null);
        Intent i = new Intent(getApplicationContext(), AjustesActivity.class);
        i.putExtra("distancia", Math.floor(MetrosRecorridos / 10));
        startActivityForResult(i, 3);
        return true;
    }
}

感谢!!!

1 个答案:

答案 0 :(得分:1)

我无法清楚地理解这个问题。但我认为线程在暂停时停止。在这种情况下,您应该为此目的创建单例计时器或将计时器用于应用程序类。在onResume方法内的活动中,启动另一个线程以刷新Textview。我希望这有帮助

Global Timer in android