2015-07-09 89 views
2

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

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

帮助!!!

的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

我不明白这个问题。但是我认为当暂停时线程停止。在这种情况下,您应该为此目的创建一个单例计时器,或者将计时器用于应用程序类。在onResume方法内的活动中,启动另一个线程来刷新Textview。我希望这可以帮助

Global Timer in android

+0

对不起,我从西班牙语和我的英语是非常糟糕的。但是,因为第一次它做得很好,并且当我第二次为线程运行该活动时? –

+0

它可能是由于多种原因发生的,例如内存泄漏,或者您尝试重新创建相同的事物,或者另一个线程可能会阻止它等等。这就是为什么您应该使用单例方法。在最糟糕的情况下,如果它不工作,您可以将计时器值保存到内部,并在恢复时重新读取并再次调用该线程。 –